Skip to content

range

Of course you can use loops to generate a range of numbers. For example, each number from 0 to 10 is generated as follows:

i = 0
while i < 10:
    print (i)
    i += 1

However, because this case occurs so frequently, there is an important abbreviation here: range:

for i in range(10):
    print (i)

In this case, range has two further parameters that enable special cases. The signature is range(start, stop, step) and means that all numbers from and including start up to and including stop are generated in steps of step. Only integers are permitted as arguments. The following two examples are equivalent:

for i in range(4, 12, 2):
    print (i)
i = 4
while i < 12:
    print (i)
    i += 2