Skip to content

Loops in Python

Loops in Python

Loops in Python helps us to repeat a specific block of code . In Python there are two types of loop one is While loop and other is For loop . Although basic functionalities of both the loops are same but syntax is different .

While loop in Python

While loop in Python is used to execute a block of code repeatedly as long as the given condition True and when the condition becomes False the program exits the loop .

Syntax :

while condition :
    statements

Example 1:

# while loop in python
number =4
while number <7:
    print(number)
    number+=1
4
5
6
  • In the above code first we have declared a variable number with value 4 .
  • Then we have written a while with condition number <7 . Whenever this condition becomes True the program will print the number and increment the number by 1 .
  • At first while will check if number <7(4<7) or not . As 4<7 is True so 4 is printed and number is incremented by 1 and become 5
  • Then as 5<7 is True so 5 is printed and number is incremented by 1 and become 6
  • Then as 6<7 is True so 6 is printed and number is incremented by 1 and become 7
  • Now 7<7 is False so program exited the loop .

Note :

In the above code we should always increment the number otherwise condition in while loop never becomes False and the loop will be an infinite loop .

To break the indefinite loop we need to click ctrl + c together .

For loop in Python

For loop in Python is used to iterate over a iterator ( string , list , tuple , set , dictionary) or iterable objects .

Syntax :

for value in iterator :
    statements

Example 1:

# for loop in python
for name in ["Rajkumar" , "Sourav" , "Sudipta"]:
    print(name)
Rajkumar
Sourav
Sudipta
  • In the above code for-loop will iterate every element in the list
  • At first , for-loop will go to the first element of the list that is "Rajkumar" and declare the name variable as "Rajkumar" and print the name variable .
  • Then , for-loop will go to the second element of the list that is "Sourav" and redeclare the name variable as "Sourav" and print the name variable .
  • After that , for-loop will go to the third element of the list that is "Sudipta" and redeclare the name variable as "Sudipta" and print the name variable .
  • when the list will end the program will exit from the for-loop .

Break statement in Python

Break statement in Python can be used to exit from the innermost loop without looping through all the items .

# break statement with while loop
number =4
while number <7:    
    number+=1 
    if number==6:
        break 
    print(number) 
5
  • In the above code we have implemented break statement with while loop
  • At first , as number is 4 so it will enter in the while loop . Then the number will be incremented by 1 and become 5 . Then it will check if the number==6 (5==6) True or not . As 5==6 is False so it will not enter in the if block and print the number
  • Now number is 5 so it will enter in the while loop .Then the number will be incremented by 1 and become 6 . Then it will check if the number==6 (6==6) True or not . As 6==6 is True so it will enter in the if block and face the break statement . After facing the break statement program will exit immediately from while loop without completing the other iterations . .

Continue statement in Python

Continue statement in Python can be used to escape the current iteration and continue with the next iteration .

Example 1:

# continue statement with while loop
number =4
while number <7:    
    number+=1 
    if number==6:
        continue 
    print(number) 
5
7
  • In the above code we have implemented continue statement with while loop
  • At first , as number is 4 so it will enter in the while loop . Then the number will be incremented by 1 and become 5 . Then it will check if the number==6 (5==6) True or not . As 5==6 is False so it will not enter in the if block and print the number .
  • Now number is 5 so it will enter in the while loop .Then the number will be incremented by 1 and become 6 . Then it will check if the number==6 (6==6) True or not . As 6==6 is True so it will enter in the if block and face the continue statement . After facing the continue statement program will escape the current iteration and continue with the next iteration .
  • Now number is 6 so it will enter in the while loop . Then the number will be incremented by 1 and become 7 . Then it will check if the number==6 (7==6) True or not . As 7==6 is False so it will not enter in the if block and print the number
  • Now number is 7 so it will not enter in the while loop .

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