Loop is used to iterate the elements of a sequence.for Loop is used to display all elements of any sequence.
When iteration starts, an element of sequence is assigned to the variable and the given statement is executed. The iteration continues till the number of elements that are there and then the control of the loop comes out of the loop.
for in loop use in python, how to use for in loop in python, what is for in loop in python?
Syntax of for Loop
for variable in sequence for_statement(s)
Example for Iterating list sequence Source Code :
a = [1, 2, 4, 5, 7, 8] for n in a : print(n)
Output :
1 2 4 5 7 8
else with for Loop
When the iteration of the elements of a sequence in the for loop is over, then the else statement; will execute
Source Code :
name = ['Rahul', 'Jyoti', 'Khushboo'] for i in name : print(i) else: print("Outside of for Loop")
Output :
Rahul Jyoti Khushboo