Skip to content

Python While Loop

Syntax

The while loop in Python is used to execute a block of code repeatedly as long as the specified condition evaluates to True.

# Generic version of a while loop
while condition:
# Code to execute while the condition is True
pass

Example

# Concrete example of a while loop
counter = 1
while counter <= 5:
print(counter)
counter += 1
# Output:
# 1
# 2
# 3
# 4
# 5