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.

The range function returns a generator—a special type of iterable that computes its values on the fly. That is why, in order to see the actual result, we need to convert it to a list.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.200.108