List of basic loop in python for while loop

2/25/2023
All Articles

#loop #python #learning #forloop #while

List of basic loop in python for while loop

Type basic loop in python 

In programming, loops are used to repeat a block of code.There are 3 basic loop in python .
While loop is usually used when the number of iterations is unknown and for loop  we provide proper value .See below example:
 

For loop in python

digits = [0, 1, 4]
 
for i in digits:
    print(i)
else:
    print("No items left.")
 
Output 
 
0
1
4
 

While loop in python

While loops in Python are used to execute block codes until a specific condition is met.

 

# initialize the variable in python 
i = 1
n = 6
 
# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
 
Output 
 
1
2
3
4
5
6
No items left.

Do while loop in python

i = 1    
while True:  
    print(i)  
    i = i + 1  
    if(i > 5):  
        break  

Article