Nearby lessons
94 of 108Python - Iterators
Detailed Tutorial Notes
24) }
25) yield person26)
27) '''''t1 = time.clock()
28) people = people_list(10000000)
29) t2 = time.clock()'''30)
31) t1 = time.clock()
32) people = people_generator(10000000)
33) t2 = time.clock()34)
35) print('Took {}'.format(t2-t1))Note: In the above program observe the differnce wrt execution time by using list and generators
Generators vs Normal Collections wrt Memory Utilization:
Normal Collection:
l=[x*x for x in range(10000000000000000)]
print(l[0])We will get MemoryError in this case because all these values are required to store in the memory.
Generators:
g=(x*x for x in range(10000000000000000))
main.py
x=888
We won't get any MemoryError because the values won't be stored at the beginning
ModulesA group of functions, variables and classes saved to a file, which is nothing but module.
Every Python file (.py) acts as a module.
Eg: durgamath.py
2)
3) def add(a,b):
4) print("The Sum:",a+b)5)
6) def product(a,b):
7) print("The Product:",a*b)durgamath module contains one variable and 2 functions.
If we want to use members of module in our program then we should import that module.
import modulenameWe can access members by using module name.
modulename.variable
modulename.function()
test.py:
main.py
888 The Sum: 30 The Product: 200