Looping statement in Python

Looping statement is available in the programming language that allows us to repeat a block of code number of times. This reduces coding work and frustration of writing same code multiple times.

They are following types of loops available in Python –

  1. While
  2. for

You can also specify else block with looping statement.

Looping statement in Python


Contents

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

 


1. While

This executes a block of code until the specified condition is true otherwise terminate the loop. It first checks the condition if its true then executes the block of code.

The execution goes until the condition is not false.

Syntax –

while expression :
      # statement

Example

num = 1

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

print("Bye")

Output

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

2. While else

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

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")

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")

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")

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")

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")

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")

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 *