Loop statements are an essential part of any programming language, as they allow us to repeat a block of code multiple times. This reduces the coding work and the frustration of writing the same code multiple times.
In Python, there are different types of loop statements that are used for various purposes.
There are the following types of loops available in Python –
- While
- for
You can also specify the else
block with a looping statement.
Contents
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 is generally used for looping over sequences (list, tuple), and 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 used.
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
5. Conclusion
Loop statement continues its execution until the condition or expression is not gets false. While adding an expression check that it will get false at some point otherwise it will become an infinite loop and slow down your application.
If you found this tutorial helpful then don't forget to share.