The range Function#
Iteration over Numbers: range()#
Rare: iteration using indexed access
Indexed access in C#
char hello[] = "Hello World";
for (int i=0; i<sizeof(hello)-1; i++)
printf("%d\n", hello[i]);
Rarely needed in Python
Iteration over data
If needed: sequence of integer numbers
hello = 'Hello World'
for i in range(len(hello)):
print(ord(hello[i]))
range(): Definition#
The range() function produces numbers …
range(100)produces0, 1, 2, ... 99range(5, 100)produces5, 6, 7, ... 99range(5, 100, 2)produces5, 7, 9, ... 99
Produces?
Result cannot easily be a list:
range(10**9)
>>> type(range(10**9))
<class 'range'>
Generates numbers on demand
⟶ Generator
range(): Python 2 vs. Python 3#
Incompatibility alert:
Python 2:
range(10**9)would explode!Heritage of the old Pre-Generator days
⟶ Python 2’s
xrange()is a generator
If one wants a list in Python 3 (unlikely) …#
l = list(range(10**9))