Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
while loop

Python while loop

 

The while loop in Python is used to iterate over a block of code as long as the condition is true.

We generally use this loop when we don't know the number of times to iterate beforehand.

 

Syntax of while Loop in Python

while condition:
   #body of a while
 

 

 

Example:

 

i = 0
while i < 5:
     print(i)
     i += 1

 

output:

 

0
1
2
3
4