Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fe13d54

Browse filesBrowse files
committed
add iter, gener and list compr
1 parent 723d490 commit fe13d54
Copy full SHA for fe13d54

File tree

Expand file treeCollapse file tree

3 files changed

+70
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+70
-0
lines changed
Open diff view settings
Collapse file

‎py3/advance/do_generator.py‎

Copy file name to clipboard
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
g = (x * x for x in range(10))
5+
print(g)
6+
for x in g:
7+
print(x)
8+
9+
def fib(max):
10+
n, a, b = 0, 0, 1
11+
while n < max:
12+
yield b
13+
a, b = b, a + b
14+
n = n + 1
15+
16+
f = fib(10)
17+
print('fib(10):', f)
18+
for x in f:
19+
print(x)
Collapse file

‎py3/advance/do_iter.py‎

Copy file name to clipboard
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
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))
8+
9+
# iter list:
10+
print('iter [1, 2, 3, 4, 5]')
11+
for x in [1, 2, 3, 4, 5]:
12+
print(x)
13+
14+
d = {'a': 1, 'b': 2, 'c': 3}
15+
16+
# iter each key:
17+
print('iter key:', d)
18+
for k in d.keys():
19+
print('key:', k)
20+
21+
# iter each value:
22+
print('iter value:', d)
23+
for v in d.values():
24+
print('value:', v)
25+
26+
# iter both key and value:
27+
print('iter item:', d)
28+
for k, v in d.items():
29+
print('item:', k, v)
30+
31+
# iter list with index:
32+
print('iter enumerate([\'A\', \'B\', \'C\']')
33+
for i, value in enumerate(['A', 'B', 'C']):
34+
print(i, value)
35+
36+
# iter complex list:
37+
print('iter [(1, 1), (2, 4), (3, 9)]:')
38+
for x, y in [(1, 1), (2, 4), (3, 9)]:
39+
print(x, y)
Collapse file

‎py3/advance/do_listcompr.py‎

Copy file name to clipboard
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
print([x * x for x in range(1, 11)])
5+
print([x * x for x in range(1, 11) if x % 2 == 0])
6+
print([m + n for m in 'ABC' for n in 'XYZ'])
7+
8+
d = {'x': 'A', 'y': 'B', 'z': 'C' }
9+
print([k + '=' + v for k, v in d.items()])
10+
11+
L = ['Hello', 'World', 'IBM', 'Apple']
12+
print([s.lower() for s in L])

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.