A for loop in Python is used to iterate over a sequence or iterable object and execute a block of code for each item in it.
Here , for is a keyword where it is used to iterate over lists, tuples, strings and other iterables.
Syntax :
Python
for variable in sequence: statement(s) Example :
Looping a List :
Python
fruits = ["apple", "banana", "mango"] for item in fruits: print(item) #output apple banana mangoLooping a String :
Python
for ch in "Python": print(ch) #output P y t h o n Loop using Range( ) :
Python
for i in range(5): print(i) #output 0 1 2 3 4 Using Else with for Loop :
Python
for i in range(3): print(i) else: print("Loop completed") #output 0 1 2 Loop completed