-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerators.py
24 lines (18 loc) · 959 Bytes
/
generators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# ------------------------------------------------------
# Example 1: add 1 to list of numbers using generators
# ------------------------------------------------------
def add_one(numbers):
for num in numbers:
yield(num + 1)
numbers = [1, 2, 3, 4, 5]
print('---------Start Example 1----------')
for num in add_one(numbers): # DO NOT CONVERT numbers (e.g. list(number) TO LIST AS PERFORMANCE GAIN WILL BE LOST.
print('Example 1:'+ str(num))
# ------------------------------------------------------
# Example 2: add 1 to list of numbers using generators
# ------------------------------------------------------
numbers = (x+1 for x in [1, 2, 3, 4, 5]) # replacing [] with () makes it generator.
# DO NOT CONVERT numbers (e.g. list(number) TO LIST AS PERFORMANCE GAIN WILL BE LOST.
print('---------Start Example 2----------')
for num in numbers:
print( 'Example 2:'+ str(num))