>>> x = [1,2,3,4,5] >>> for i in x: ... i = 0 ... >>> x [1, 2, 3, 4, 5]The value of the elements in

>>> dblarray = [[7,12,9],[13,8,3],[19,2,14]] >>> for d in dblarray: ... d[0] = 0 ... >>> dblarray [[0, 12, 9], [0, 8, 3], [0, 2, 14]]

Another example is the case of creating a new sequence by processing one or more
other sequences. Suppose we have an array of prices and an array of the same
length containing taxes, and we wish to create a third array which has the total
of the prices and the taxes for each of the items represented in the two arrays.
Clearly, the `for` loop as presented so far has no means of dealing with this
problem. If you're familiar with other programming languages, you'll notice that
these two tasks are quite simple in most other languages, at the cost of either
a complex syntax or additional looping constructs.

In python, problems like this are solved by iterating over a sequence of integers
created by the `range` function, and then refering to the individual elements
of the sequence inside the body of the `for` loop to perform the desired
tasks. The `range` function accepts one, two or three arguments.
With a single integer argument, `range` returns a sequence of integers from 0 to
one less than the argument provided.
With two arguments, the first argument is
used as a starting value instead of 0, and with three arguments, the third argument
is used as an increment instead of the implicit default of 1.
Notice that `range` never includes its upper limit in the returned sequence;
like slices (Section 2.4.3), it is designed to work in conjunction with
the subscripting of elements in a sequence, which start at 0 and continue to one less
than the length of the sequence.

So to zero out all the elements in an array, we could use the `range` as follows:

>>> x = [1,2,3,4,5] >>> for i in range(len(x)): ... x[i] = 0 ... >>> x [0, 0, 0, 0, 0]Since the

>>> prices = [12.00,14.00,17.00] >>> taxes = [0.48,0.56,0.68] >>> total = [] >>> for i in range(len(prices)): ... total.append(prices[i] + taxes[i]) ... >>> total [12.48, 14.56, 17.68]Alternatively, we could refer to the elements of

>>> total = len(prices) * [0] >>> for i in range(len(prices)): ... total[i] = prices[i] + taxes[i] ... >>> total [12.48, 14.56, 17.68]The brackets around the

The `range` function actually produces a list in memory containing all the
elements specified through its arguments. For sequences involving very large numbers,
this may consume large
amounts of memory. In cases like this, python provides the `xrange`
function. While this function returns an object that behaves just like the list
returned by `range`, the appropriate elements are calculated as needed, rather
than actually stored in memory. Since this makes `xrange` considerably
slower than `range`, you should only resort to `xrange` when
working with a large range of values.