1def count_to_ten_generator():
2 for number in range(10):
3 yield number
4my_generator = count_to_ten_generator()
5first_number = next(my_generator)
6list_or_the_rest = list(my_generator)
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")