GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — update iter sample · WangLeGeWang/learn-python3@aed8fae

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • samples/advance
    • do_iter.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎samples/advance/do_iter.py‎

Copy file name to clipboardExpand all lines: samples/advance/do_iter.py
+30-5Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
from collections import Iterable
5-
print('iterable? [1, 2, 3]:', isinstance([1, 2, 3], Iterable))
6-
print('iterable? \'abc\':', isinstance('abc', Iterable))
7-
print('iterable? 123:', isinstance(123, Iterable))
4+
from collections import Iterable, Iterator
5+
6+
def g():
7+
yield 1
8+
yield 2
9+
yield 3
10+
11+
print('Iterable? [1, 2, 3]:', isinstance([1, 2, 3], Iterable))
12+
print('Iterable? \'abc\':', isinstance('abc', Iterable))
13+
print('Iterable? 123:', isinstance(123, Iterable))
14+
print('Iterable? g():', isinstance(g(), Iterable))
15+
16+
print('Iterator? [1, 2, 3]:', isinstance([1, 2, 3], Iterator))
17+
print('Iterator? iter([1, 2, 3]):', isinstance(iter([1, 2, 3]), Iterator))
18+
print('Iterator? \'abc\':', isinstance('abc', Iterator))
19+
print('Iterator? 123:', isinstance(123, Iterator))
20+
print('Iterator? g():', isinstance(g(), Iterator))
821

922
# iter list:
10-
print('iter [1, 2, 3, 4, 5]')
23+
print('for x in [1, 2, 3, 4, 5]:')
1124
for x in [1, 2, 3, 4, 5]:
1225
print(x)
1326

27+
print('for x in iter([1, 2, 3, 4, 5]):')
28+
for x in iter([1, 2, 3, 4, 5]):
29+
print(x)
30+
31+
print('next():')
32+
it = iter([1, 2, 3, 4, 5])
33+
print(next(it))
34+
print(next(it))
35+
print(next(it))
36+
print(next(it))
37+
print(next(it))
38+
1439
d = {'a': 1, 'b': 2, 'c': 3}
1540

1641
# iter each key:

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.