Skip to content

Python For Loop

Python For Loop

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 declare letter variable as that letter and print the letter variable .
  • At first from "Rajkumar" for-loop iterate "R" and declare letter variable as "R" and print the letter variable
  • Then from "Rajkumar" for-loop iterate "a" and redeclare letter variable as "a" and print the letter 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 a tuple . For this reason we have declared a variable myTuple as ("Rajkumar" , "Sourav" , "Sudipta") .
  • Then we have iterated all the elements of the tuple using for 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 a dictionary . For this reason we have declared a variable myDict as {"Python":3,"Java":8,"JavaScript":6} .
  • Then we have iterated all the elements of the dictionary using for loop and printed them . We can see that only key of the dictionary 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 the for loop will be exited forcefully and the code in else block will not be executed .
  • But if program do not face break statement then for loop will be completed successfully and the code in else 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 the fruits variable and check if "apple" is present or not .
  • As "apple" is present in fruits list so first program printed We found apple in the output screen and then faced the break statement and the program exited from for loop forcefully .
  • As program exited from for loop forcefully so it will not enter in the else 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 the fruits variable and check if "apple" is present or not .
  • As "apple" is not present in fruits list so for loop will iterate all the elements and the program will exit from for loop normally .
  • As program exited from for loop normally so it will enter in the else block and print We 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 and fruits as ["big" , "medium" , "small"] and ["guava","banana","pineapple","mango"] respectively .
  • Then outer loop will iterate first element("big") from sizes variable and inner loop will iterate all the elements one after another from fruits variable
  • Then outer loop will iterate second element("medium") from sizes variable and inner loop will iterate all the elements one after another from fruits 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 .

Leave a Reply