
P1: JYS
app01 JWBK378-Fletcher May 1, 2009 19:56 Printer: Yet to come
198 Financial Modelling in Python
range(...)
range([start,] stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults
to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is
omitted!
These are exactly the valid indices for a list of 4 elements.
>>>
Now returning to our understanding of the for statement example above:
>>> for i in range(10):
... print i
...
Note how the print i is indented relative to the preceding line (for i in ...). This is
significant. That is, white space is significant in Python and its use indicates where different
parts of statements begin and end much like the use of ‘{’ and ‘}’ in C/C++. To illustrate
further, here’s a more involved snippet:
>>> for i in range(0, 2):
... print i
... for j in range(0, 10):
... print " %d" % (j)
... print
...
0
0
1
2
3
4
5
6
7
8
9
1
0
1
2
3
4
5
6
7
8
9
>>>