December 2018
Beginner to intermediate
796 pages
19h 54m
English
Sometimes we need to iterate over a range of numbers, and it would be quite unpleasant to have to do so by hardcoding the list somewhere. In such cases, the range function comes to the rescue. Let's see the equivalent of the previous snippet of code:
# simple.for.pyfor number in range(5):
print(number)
The range function is used extensively in Python programs when it comes to creating sequences: you can call it by passing one value, which acts as stop (counting from 0), or you can pass two values (start and stop), or even three (start, stop, and step). Check out the following example:
>>> list(range(10)) # one value: from 0 to value (excluded)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> list(range(3, 8)) # two values: from start ...
Read now
Unlock full access