range – a collection of evenly spaced integers

Let's move on and look at range, which many developers wouldn't consider to be a collection, although we'll see that in Python 3 it most definitely is.

A range is a type of sequence used for representing an arithmetic progression of integers. Ranges are created by calls to the range() constructor, and there is no literal form. Most typically we supply only the stop value, as Python defaults to a starting value of zero:

>>> range(5)
range(0, 5)

Ranges are sometimes used to create consecutive integers for use as loop counters:

>>> for i in range(5):
... print(i)
...
0
1
2
3
4

Note that the stop value supplied to range() is one past the end of the sequence, which is why the previous loop didn't print 5.

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

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