Range¶
range() returns an immutable sequence of numbers, which is often used as a range generator for the for loop.
In [4]:
r1: range = range(11) # Returns a sequence of numbers from 0 to 10
r2: range = range(5, 21) # Returns a sequence of numbers from 5 to 20
r3: range = range(20, 9, -2) # Returns a sequence of numbers from 20 to 10 with a step of 2
print('To exclusive: ', end='')
for i in r1:
print(f"{i} ", end='')
print('\nFrom inclusive to exclusive: ', end='')
for i in r2:
print(f"{i} ", end="")
print('\nFrom inclusive to exclusive with step: ', end='')
for i in r3:
print(f'{i} ', end='')
print(f'\nFrom = {r3.start}')
print(f'To = {r3.stop}')
To exclusive: 0 1 2 3 4 5 6 7 8 9 10 From inclusive to exclusive: 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 From inclusive to exclusive with step: 20 18 16 14 12 10 From = 20 To = 9