-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path46_Generators.py
More file actions
38 lines (27 loc) · 978 Bytes
/
46_Generators.py
File metadata and controls
38 lines (27 loc) · 978 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
"""A generator function is a special type of function that returns an iterator object.
Instead of using return to send back a single value, generator functions use yield to produce a series of results over time."""
def fun(max):
cnt = 1
while cnt <= max:
yield cnt
cnt += 1
ctr = fun(5)
for n in ctr:
print(n)
# Creating Generators -> Creating a generator in Python is as simple as defining a function with at least one yield statement.
def fun():
yield 1
yield 2
yield 3
# Driver code to check above generator function
for val in fun():
print(val)
# Yield vs Return
def fun():
return 1 + 2 + 3
res = fun()
print(res)
# Generator Expression -> Generator expressions are a concise way to create generators. They are similar to list comprehensions but use parentheses instead of square brackets and are more memory efficient
sq = (x*x for x in range(1, 6))
for i in sq:
print(i)