- Learn Python by Building Data Science Applications
- Philipp Kats David Katz
- 204字
- 2021-06-24 13:06:03
The range function
The range function generates a sequence of integers. It takes from one to three arguments, and each of those is expected to be an integer. If only one is defined, it will be treated as a right-hand limit of the range and will be not included. If two are defined—the first one will be the left-hand limit, and included in the range, while the second will take the role of the right-hand limit. If a third value is specified, it will be used as a step—the default step is equal to 1.
Let's look at the following example. Here, we generate a range of three values. We convert the outcome to a list in order to show the values:
>>> list(range(3))
[0, 1, 2]
>>> list(range(2, 5))
[2, 3, 4]
In the second example, we pass two arguments instead. In this case, the first argument defines the starting value, and the range will go up until the second argument.