For Loop
Python for Loops
Section titled “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.
listtuplesetrange
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).
for
for x in sequence: statement1 statement2 ... statementNTo 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.
x``:``x
The following example demonstrates the for loop with the list object.
list
nums = [10, 20, 30, 40, 50]
for i in nums: print(i)`nums = [10, 20, 30, 40, 50]
for i in nums: print(i)`Try it
10 20 30 40 5010 20 30 40 50
The following demonstrates the for loop with a tuple object.
nums = (10, 20, 30, 40, 50)for i in nums: print(i)nums = (10, 20, 30, 40, 50) for i in nums: print(i)Try it
10 20 30 40 5010 20 30 40 50
The object of any Python sequence data type can be iterated using the for statement.
for char in 'Hello': print (char)for char in 'Hello': print (char)Try it
H e l l oH e l l o
The following for loop iterates over the dictionary using the items() method.
fordictionaryitems()
numNames = { 1:'One', 2: 'Two', 3: 'Three'}
for pair in numNames.items(): print(pair)`numNames = { 1:‘One’, 2: ‘Two’, 3: ‘Three’}
for pair in numNames.items(): print(pair)`Try it
(1, 'One') (2, 'Two') (3, 'Three')(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.
for
numNames = { 1:'One', 2: 'Two', 3: 'Three'}
for k,v in numNames.items(): print("key = ", k , ", value =", v)`numNames = { 1:‘One’, 2: ‘Two’, 3: ‘Three’}
for k,v in numNames.items(): print(“key = ”, k , ”, value =”, v)`Try it
key = 1, value = One key = 2, value = Two key = 3, value = Threekey = 1, value = One key = 2, value = Two key = 3, value = Three
For Loop with the range() Function
Section titled “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.
rangerange()range``for
for i in range(5): print(i)for i in range(5): print(i)Try it
0 1 2 3 40 1 2 3 4
Exit the For Loop
Section titled “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.
break
for i in range(1, 5): if i > 2 : break print(i)for i in range(1, 5): if i > 2 : break print(i)Try it
1 21 2
Continue Next Iteration
Section titled “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.
continue``continue
for i in range(1, 5): if i > 3: continue print(i)for i in range(1, 5): if i > 3: continue print(i)Try it
1 2 31 2 3
For Loop with Else Block
Section titled “For Loop with Else Block”The else block can follow the for loop, which will be executed when the for loop ends.
else``for``for
for i in range(2): print(i)else: print('End of for loop')for i in range(2): print(i) else: print('End of for loop')Try it
0 1 End of for loop0 1 End of for loop
Nested for Loop
Section titled “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.
for x in range(1,4): for y in range(1,3): print('x = ', x, ', y = ', y)for x in range(1,4): for y in range(1,3): print('x = ', x, ', y = ', y)Try it
x = 1, y = 1 x = 1, y = 2 x = 2, y = 1 x = 2, y = 2 x = 3, y = 1 x = 3, y = 2x = 1, y = 1 x = 1, y = 2 x = 2, y = 1 x = 2, y = 2 x = 3, y = 1 x = 3, y = 2