forked from michaelliao/learn-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdo_iter.py
More file actions
executable file
·39 lines (31 loc) · 883 Bytes
/
do_iter.py
File metadata and controls
executable file
·39 lines (31 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import Iterable
print('iterable? [1, 2, 3]:', isinstance([1, 2, 3], Iterable))
print('iterable? \'abc\':', isinstance('abc', Iterable))
print('iterable? 123:', isinstance(123, Iterable))
# iter list:
print('iter [1, 2, 3, 4, 5]')
for x in [1, 2, 3, 4, 5]:
print(x)
d = {'a': 1, 'b': 2, 'c': 3}
# iter each key:
print('iter key:', d)
for k in d.keys():
print('key:', k)
# iter each value:
print('iter value:', d)
for v in d.values():
print('value:', v)
# iter both key and value:
print('iter item:', d)
for k, v in d.items():
print('item:', k, v)
# iter list with index:
print('iter enumerate([\'A\', \'B\', \'C\']')
for i, value in enumerate(['A', 'B', 'C']):
print(i, value)
# iter complex list:
print('iter [(1, 1), (2, 4), (3, 9)]:')
for x, y in [(1, 1), (2, 4), (3, 9)]:
print(x, y)