Loops in Python

While writing the program there will be some situation where we need to repeat a block of code number of time.

For handling this types of situation we generally use loops. Using loops we execute a block of code until the expression returns true.

They are following types of loops available in Python –

  1. While
  2. for

 


Contents

  1. While
  2. While else
  3. For
  4. For else

 


1. While

While loop executes a block of code until specified condition is true otherwise terminate the loop. It first checks the condition if condition is  true then executes the block of code inside the loop then again check the condition until the condition is not false.

Syntax –

while expression :
      # statement

Example –

num = 1

while num<=5 :
      print("num : ",num)
      num+=1

print("Bye")

When we execute the above program, it will produce the following output –

num : 1
num : 2
num : 3
num : 4
num : 5
Bye

2. While else

Python allows us to define else with loop also. else block executes only when the loop terminates normally not by a break.

Syntax –

while expression :
     # statement
else:
     # else statement

Example –

Without break keyword

num = 1

while num<=5 :
      print("num : ",num)
      num+=1
else:
      print(num,"is greater than 5")
print("Bye")

When we execute the above program, it will produce the following output –

num : 1
num : 2
num : 3
num : 4
num : 5
6 is greater than 5
Bye

With break keyword

num = 1

while num<=5 :
      print("num : ",num)
      if num == 3:
         break
      num+=1
else:
      print(num,"is greater than 5")
print("Bye")

When we execute the above program, it will produce the following output –

num : 1
num : 2
num : 3
Bye

3. For

For loop generally used for looping over sequences(list,tuple), dictionary. When for loop executes it stores the first item in loop variable and execute the block code next time it takes next item from sequence and stores it in loop variable until the sequence is not empty.

Syntax –

for variable in sequence :
    # statement

Example –

names = ["Sonarika","Yogesh","Vishal","Anil"]

for n in names :
      print("name : ",n)

print("Bye")

When we execute the above program, it will produce the following output –

name : Sonarika
name : Yogesh
name : Vishal
name : Anil
Bye

For iterating for loop with numeric value range method is use.

Example –

names = ["Sonarika","Yogesh","Vishal","Anil"]

for n in range(len(names)):
      print("name : ",name[n])

print("Bye")

When we execute the above program, it will produce the following output –

name : Sonarika
name : Yogesh
name : Vishal
name : Anil
Bye

4. For else

else work same as it does in the while loop. It only executes when loop terminates normally not by using break statement.

Syntax –

for variable in sequence :
    # statement
else:
    # false statement

Example –

Without break keyword

names = ["Sonarika","Yogesh","Vishal","Anil"]

for n in names :
      print("name : ",n)
else: 
      print("List is empty")
print("Bye")

When we execute the above program, it will produce the following output –

name : Sonarika
name : Yogesh
name : Vishal
name : Anil
List is empty
Bye

With break keyword

names = ["Sonarika","Yogesh","Vishal","Anil"]

for n in names :
      if n == "Vishal":
           break
      print("name : ",n)
else:
     print("List is empty")
print("Bye")

When we execute the above program, it will produce the following output –

name : Sonarika
name : Yogesh
Bye
Spread the love

Related Post

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *