Loading, please wait...

Python – While Loop

Python has two primitive loop commands:

  • while loops
  • for loops

The while Loop

With the while loop you can execute a set of statements as long as a condition is true.

Example:

Print i as long as i is less than 6:

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

Output

C:\Users\My Name>python demo_while.py
1
2
3
4