Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Generators - my_range

import sys

def my_range(limit = 1):
    n = 0
    while n < limit:
        yield n
        n += 1

for i in my_range(5):
    print(i)
print()

print(sum(my_range(10)))
print()

x = my_range(10000)
print(x)
print(sys.getsizeof(x))

Output:

0
1
2
3
4

45

<generator object my_range at 0x7f36f6089930>
120