Page Contents
Python For Loop is used to iterate over a iterator ( string , list , tuple , set , dictionary) or iterable objects .
Syntax of for loop in Python
for value in iterator : statements
Structure of for loop in Python
- Python for loop starts with a
for
keyword followed by a statement and:
(colon) - Then we need to write a block of code with proper indentation .
For loop in Python Examples
Example 1: Iterate a string in Python
# for loop with string as iterator
myString = "Rajkumar"
for letter in myString:
print(letter)
R a j k u m a r
- In the above code we have used for loop with string . For this reason we have declared a variable
myString
as"Rajkumar"
- After that from
"Rajkumar"
for-loop
iterate every letter and declareletter
variable as that letter and print theletter
variable . - At first from
"Rajkumar"
for-loop
iterate"R"
and declareletter
variable as"R"
and print theletter
variable - Then from
"Rajkumar"
for-loop
iterate"a"
and redeclareletter
variable as"a"
and print theletter
variable . - And the process go on until the string ends .
Example 2: Iterate a tuple in Python
# for loop with tuple as iterator
myTuple=("Rajkumar" , "Sourav" , "Sudipta")
for name in myTuple:
print(name)
Rajkumar Sourav Sudipta
- In the above code we have used
for
loop with atuple
. For this reason we have declared a variablemyTuple
as("Rajkumar" , "Sourav" , "Sudipta")
. - Then we have iterated all the elements of the
tuple
usingfor
loop and printed them .
Note :
for
loop behaves same with list
and set
like tuple
.
Example 3: Iterate a dictionary in Python
# for loop with dictionary as iterator
myDict={"Python":3,"Java":8,"JavaScript":6}
for language in myDict:
print(language)
Python Java JavaScript
- In the above code we have used
for
loop with adictionary
. For this reason we have declared a variablemyDict
as{"Python":3,"Java":8,"JavaScript":6}
. - Then we have iterated all the elements of the
dictionary
usingfor
loop and printed them . We can see that only key of thedictionary
is printed .
Example 4: Iterate a range datatype in Python
# for loop with range as iterator
for number in range(5):
print(number)
0 1 2 3 4
In the above code we have used for
loop with range
datatype .
To learn more about range
datatype click here –
For-Else loop in Python
We can use else
keyword with Python for
loop . For this reason we need to use break statement .
Syntax of for else loop in Python:
for value in iterator : statements break else : statements
- If program face
break
statement then thefor
loop will be exited forcefully and the code inelse
block will not be executed . - But if program do not face
break
statement thenfor
loop will be completed successfully and the code inelse
block will be executed .
Example 1:
# for-else loop in python
fruits =["guava" , "apple" , "pineapple" , "mango"]
for fruit in fruits :
if fruit=="apple":
print("We found apple")
break
else :
print("We don't found apple")
We found apple
- In the above code first we have declared a
fruits
variable as["guava" , "apple" , "pineapple" , "mango"]
- Then
for
loop will iterate all the elements of thefruits
variable and check if"apple"
is present or not . - As
"apple"
is present infruits
list so first program printedWe found apple
in the output screen and then faced thebreak
statement and the program exited fromfor
loop forcefully . - As program exited from
for
loop forcefully so it will not enter in theelse
block .
Example 2:
# for-else loop in python
fruits =["guava" , "banana" , "pineapple" , "mango"]
for fruit in fruits :
if fruit=="apple":
print("We found apple")
break
else :
print("We don't found apple")
We don't found apple
- In the above code first we have declared a
fruits
variable as["guava" , "banana" , "pineapple" , "mango"]
- Then
for
loop will iterate all the elements of thefruits
variable and check if"apple"
is present or not . - As
"apple"
is not present infruits
list sofor
loop will iterate all the elements and the program will exit fromfor
loop normally . - As program exited from
for
loop normally so it will enter in theelse
block and printWe don't found apple
.
Nested Python for loops
Nested Python for loops means a for loop inside a for loop . For every iteration of outer for loop inner for loop will be executed one time .
Example 1:
# nested for loop
sizes =["big" , "medium" , "small"]
fruits =["guava","banana","pineapple","mango"]
for size in sizes: # outer for loop
for fruit in fruits : # inner for loop
print(size , fruit)
big guava big banana big pineapple big mango medium guava medium banana medium pineapple medium mango small guava small banana small pineapple small mango
- In the above code we have applied nested python
for
loops . - At first , we have declared two variables
sizes
andfruits
as["big" , "medium" , "small"]
and["guava","banana","pineapple","mango"]
respectively . - Then outer loop will iterate first element(
"big"
) fromsizes
variable and inner loop will iterate all the elements one after another fromfruits
variable - Then outer loop will iterate second element(
"medium"
) fromsizes
variable and inner loop will iterate all the elements one after another fromfruits
variable - And the process will go on until the outer loop ends .
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .