Python For Loop

The for loop statement iterates over a range of values or sequence. These values can be a numeric range, or, elements of a data structure like a string, list or tuple. So, the use of 'for' construct is to do some repetitive operation for each given number of a sequence.

Syntax of For Loop

for variable in sequence:

The sequence can be any expression, such as a tuple or a list.

Example1 of For Loop

>>> for n in range(1, 6):


Python for loop

Output of the above code
Python for loop

Example2 of For Loop

Here is the example to print the cubes of all numbers from 1 through 5.

>>> for n in range(1,6):
	print "The cube of {0} is {1}".format(n, n**3)

The cube of 1 is 1
The cube of 2 is 8
The cube of 3 is 27
The cube of 4 is 64
The cube of 5 is 125