1# Size of generators is a huge advantage compared to list
2import sys
3
4n= 80000
5
6# List
7a=[n**2 for n in range(n)]
8
9# Generator
10# Be aware of the syntax to create generators, lika a list comprehension but with round brakets
11b=(n**2 for n in range(n))
12
13print(f"List: {sys.getsizeof(a)} bits\nGenerator: {sys.getsizeof(b)} bits")
1def mygenerator():
2 print('First item')
3 yield 10
4
5 print('Second item')
6 yield 20
7
8 print('Last item')
9 yield 30
10