range() function

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 zero to ten, or counting 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 else 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

range() Abbreviated Syntax

range() also has a simple format, which takes one or both integer arguments:
range( start=0, end)
Given both values, this shortened version of range() is exactly the same as the long version of range() taking two parameters with step defaulting to 1. However, if given only a single value, start defaults to zero, and range() returns a list of numbers from zero up to the argument end:

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

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

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