Tutorialsteacher

Follow Us

Python for Loops

In Python, the for loop is used for iterating over sequence types such as list, tuple, set, range, etc. Unlike other programming language, it cannot be used to execute some code repeatedly.

The body of the for loop is executed for each member element in the sequence. Hence, it doesn't require explicit verification of a boolean expression controlling the loop (as in the while loop).

Syntax:
for x in sequence: statement1 statement2 ... statementN

To start with, a variable x in the for statement refers to the item at the 0 index in the sequence. The block of statements with increased uniform indent after the : symbol will be executed. A variable x now refers to the next item and repeats the body of the loop till the sequence is exhausted.

The following example demonstrates the for loop with the list object.

Example:
nums = [10, 20, 30, 40, 50]

for i in nums:
    print(i)
Output
10 20 30 40 50

The following demonstrates the for loop with a tuple object.

Example: For Loop with Tuple
nums = (10, 20, 30, 40, 50)
for i in nums:
    print(i)
Output
10 20 30 40 50

The object of any Python sequence data type can be iterated using the for statement.

Example: For Loop with String
for char in 'Hello':
    print (char)
Output
H e l l o

The following for loop iterates over the dictionary using the items() method.

Example: For Loop with Dictionary
numNames = { 1:'One', 2: 'Two', 3: 'Three'}

for pair in numNames.items():
    print(pair)
Output
(1, 'One') (2, 'Two') (3, 'Three')

The key-value paris can be unpacked into two variables in the for loop to get the key and value separately.

Example: For Loop with Dictionary
numNames = { 1:'One', 2: 'Two', 3: 'Three'}

for k,v in numNames.items():
    print("key = ", k , ", value =", v)
Output
key = 1, value = One key = 2, value = Two key = 3, value = Three

For Loop with the range() Function

The range class is an immutable sequence type. The range() returns the range object that can be used with the for loop.

Example:
for i in range(5):
    print(i)
Output
0 1 2 3 4

Exit the For Loop

The execution of the for loop can be stop and exit using the break keyword on some condition, as shown below.

Example:
for i in range(1, 5):
    if i > 2 :
        break
    print(i)
Output
1 2

Continue Next Iteration

Use the continue keyword to skip the current execution and continue on the next iteration using the continue keyword on some condition, as shown below.

Example:
for i in range(1, 5):
    if i > 3:
        continue
    print(i)
Output
1 2 3

For Loop with Else Block

The else block can follow the for loop, which will be executed when the for loop ends.

Example:
for i in range(2):
    print(i)
else:
     print('End of for loop')
Output
0 1 End of for loop

Nested for Loop

If a loop (for loop or while loop) contains another loop in its body block, we say that the two loops are nested. If the outer loop is designed to perform m iterations and the inner loop is designed to perform n repetitions, the body block of the inner loop will get executed m X n times.

Example: Nested for loop
for x in range(1,4):
    for y in range(1,3):
        print('x = ', x, ', y = ', y)
Output
x =  1, y =  1 x =  1, y =  2 x =  2, y =  1 x =  2, y =  2 x =  3, y =  1 x =  3, y =  2