8.6.4 range() Built-in Function

We mentioned above during our introduction to Python’s for loop that it is an iterative looping mechanism. Python also provides a tool that will let us use the for statement in a traditional pseudo-conditional setting, i.e., when counting from one number to another and quitting once the final number has been reached or some condition is no longer satisfied.

The built-in function range() can turn your foreach-like for loop back into one that you are more familiar with, i.e., counting from 0 to 10, or count- ing from 10 to 100 in increments of 5.

range() Full Syntax

Python presents two different ways to use range(). The full syntax requires that two or all three integer arguments are present:
range(start, end, step =1)
range() will then return a list where for any k, start <= k < end and k iterates from start to end in increments of step. step cannot be 0, or an error condition will occur.
>>> range(2, 19, 3)
[2, 5, 8, 11, 14, 17]

If step is omitted and only two arguments given, step takes a default value of 1.

>>> range(3, 7)
[3, 4, 5, 6]


Let’s take a look at an example used in the interpreter environment:

>>> for eachVal in range(2, 19, 3):
... print "value is:", eachVal
value is: 2 value is: 5 value is: 8 value is: 11 value is: 14 value is: 17

Our for loop now “counts” from 2 to 19, incrementing by steps of 3. If you are familiar with C, then you will notice the direct correlation between the arguments of range() and those of the variables in the C for loop:

/* equivalent loop in C */
for (eachVal = 2; eachVal < 19; eachVal += 3) {
printf("value is: %d\n", eachVal);

Although it seems like a conditional loop now (checking if eachVal<19), reality tells us that range() takes our conditions and generates a list that meets our criteria, which in turn is used by the same Python for statement.

range() Abbreviated Syntax
range() also has two abbreviated syntax formats:


range(start, end)

We saw the shortest syntax earlier in Chapter 2. Given only a single value, start defaults to 0, step defaults to 1, and range() returns a list of num- bers from zero up to the argument end:

>>> range(5)
[0, 1, 2, 3, 4]

Given two values, this midsized version of range() is exactly the same as the long version of range() taking two parameters with step defaulting to

1. We will now take this to the Python interpreter and plug in for and print
statements to arrive at:

>>> for count in range(2, 5):
... print count


CORE NOTE: Why not just one syntax for range()?

Now that you know both syntaxes for range(), one nagging question
you may have is, why not just combine the two into a single one that looks
like this?

range(start=0, end, step =1) # invalid
This syntax will work for a single argument or all three, but not two. It is illegal because the presence of step requires start to be given. In other words, you cannot provide end and step in a two-argument version
because they will be (mis)interpreted as start and end.


8.6.5 xrange() Built-in Function

xrange() is similar to range() except that if you have a really large range list, xrange() may come in handier because it does not have to make a com- plete copy of the list in memory. This built-in was made for exclusive use in for loops. It does not make sense outside a for loop. Also, as you can imag- ine, the performance will not be as good because the entire list is not in memory. In future versions of Python, range() will eventually become like xrange(), returing an iterable object (not a list nor an iterator though)—it will be similar to views as discussed in the previous chapter.