Python For Loops:
- Used to iterate over a sequence (list, tuple, string) or other iterable objects.
- Iterating over a sequence is called traversal.
Table Of Contents
For Loop Syntax:
Syntax:
for element in data: Body of for loop
Here, element is the variable that takes the value of the item inside the sequence on each iteration. Loop continues unless we reach the last item in the sequence.
Example:
Find the product of all numbers present in the list.
x=[10,20,30,40,50] product=1 for element in x: product*=element print(“Product is: {}”.format(product))
Output:
Product is: 12000000
Range Function[range()]:
Syntax:
range(start, stop, step)
This function does not store all the values in memory, it would be inefficient. So it remembers the start, stop, and step size and generates the next number on the go.
Example
for i in range(6): print(i)
Output:
0 1 2 3 4 5
Example
for i in range(1,20,2): print(i)
Output:
1 3 5 7 9 11 13 15 17 19
Example
array =['Orange', 'Apple', 'Mango', 'Banana', 'Grapes'] for data in range(len(array)): print(array[data])
Output:
Orange Apple Mango Banana Grapes
Example
array =['Orange', 'Apple', 'Mango', 'Banana', 'Grapes'] for data in array: print(data)
Output:
Orange Apple Mango Banana Grapes
For Loop With Else:
Example
fruits =['Apple', 'Mango', 'Banana'] for elements in fruits: print(elements) else: print(‘No Item Left’)
Output:
Apple Mango Banana No Item Left
Break Statement:
- Break Statement is used to stop the execution of the loop and come out of it and go to the next statement’s execution directly.
- It is used in both for loop and while loop.
Syntax
for element in data: #code inside for loop if condition: break #code inside for loop #code outside for loop
Example
array = [1,2,3,4] for data in array: if data==3: break print(data) else: print('Inside the else block') print('Outside The For Loop.')
Output:
1
2
Outside The For Loop.
Continue Statement:
- Continue Statement takes us to the next iteration directly.
- It is also used in both for loop and while loop.
Syntax
for element in data: #code inside for loop if condition: continue #code inside for loop #code outside for loop
Example
array = [1,2,3,4] for data in array: if data==3: continue print(data) else: print('Inside The Else Block.')
Output:
1 2 4 Inside The Else Block.
Note: Break statement comes out of else statement also whereas continue statement prints the else part too.
Nested For Loop:
A loop inside a loop is called the nested loop. For each iteration of the outer loop, the inner loop will be executed once.
Syntax
for i in range(r1): #First For Loop for j in range(r2): #Second For Loop Inside First For Loop Write Content Here
Example:
Pattern Print Using Nested For Loop.
n=8 for i in range(n): for j in range(i+1): print('*',end=' ') print()
Output:
Pass Statement:
- If for some reason we have a “for loop” with no content, put it in the pass statement to avoid getting an error.
- It is performing like a null operation.
Example
for x in [1,2,3,4,5,6,7,8]: pass