
The Python while loop is one of the most important building blocks in any Python program. It lets you run a block of code repeatedly as long as a condition you define stays true. Instead of writing the same lines over and over, you write them once inside a Python while loop and let Python handle the repetition. This guide covers everything you need to know about the Python while loop — how it works, how conditions control it, and how to use it in practical situations.
The syntax of a Python while loop is clean and easy to read. You write the keyword while, followed by a condition, followed by a colon. Every line indented underneath that forms the loop body, and it runs each time the condition is True.
while condition:
# code to run while condition is True
Python checks the condition before every iteration. If it is True, the body runs. After the body finishes, Python checks the condition again. This repeats until the condition is False, at which point execution moves to whatever comes after the loop.
count = 1
while count <= 5:
print("Count:", count)
count = count + 1
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Python checks count <= 5 before each pass. On the first pass count is 1, which satisfies the condition, so the body runs and count becomes 2. This continues until count reaches 6, at which point the condition is False and the while loop stops.
The condition in a Python while loop is evaluated as a boolean expression — it is either True or False. You can use any comparison operator to build this condition: less than, greater than, less than or equal to, greater than or equal to, equal to, or not equal to.
speed = 120
while speed > 60:
print("Speed:", speed, "km/h - slowing down")
speed = speed - 20
print("Safe speed reached:", speed, "km/h")
Speed: 120 km/h - slowing down
Speed: 100 km/h - slowing down
Speed: 80 km/h - slowing down
Speed: 60 km/h - Safe speed reached: 60 km/h
When speed drops to 60, the condition speed > 60 becomes False and the while loop exits. The line after the loop then prints once. This shows how the condition is the single gate controlling when the loop runs and when it stops.
The condition is checked at the start of each iteration, not in the middle. This means if the condition is already False before the loop even starts, the body never runs at all:
number = 10
while number > 20:
print("This will never print")
print("Loop was skipped because condition was False from the start")
Loop was skipped because condition was False from the start
Python sees that 10 > 20 is False immediately and skips the body entirely, jumping straight to the code after the loop.
The most common pattern with a Python while loop is using a counter variable. You initialize the counter before the loop, use it in the condition, and update it inside the loop body on every iteration.
counter = 1
while counter <= 8:
print(counter, "x 3 =", counter * 3)
counter = counter + 1
1 x 3 = 3
2 x 3 = 6
3 x 3 = 9
4 x 3 = 12
5 x 3 = 15
6 x 3 = 18
7 x 3 = 21
8 x 3 = 24
The counter starts at 1, the condition checks it is less than or equal to 8, the loop body prints the multiplication result, and then counter increases by 1. This pattern repeats cleanly and predictably.
It is equally common to count downward. You initialize the counter high, use a greater-than condition, and subtract inside the loop:
countdown = 10
while countdown >= 1:
print(countdown)
countdown = countdown - 1
print("Launched!")
10
9
8
7
6
5
4
3
2
1
Launched!
Here countdown decreases by 1 on each pass until it reaches 0, which fails the condition countdown >= 1, and the loop stops.
A very practical use of the Python while loop is accumulating a running total. You initialize a total variable to zero, then add to it on each iteration as the loop runs through a range of values.
total = 0
number = 1
while number <= 100:
total = total + number
number = number + 1
print("Sum of 1 to 100:", total)
Sum of 1 to 100: 5050
The while loop runs 100 times. On each pass, the current value of number is added to total and then number increases by 1. When number reaches 101, the condition is False and the loop exits with total holding the sum of all numbers from 1 to 100.
You can use the same accumulation pattern for multiplication:
result = 1
factor = 1
while factor <= 6:
result = result * factor
factor = factor + 1
print("6 factorial:", result)
6 factorial: 720
Each iteration multiplies result by the current factor. After six iterations the result holds 6 factorial, which is 720.
You can combine more than one condition in a Python while loop using the and and or logical operators. The loop keeps running only when the combined expression evaluates to True.
x = 0
y = 50
while x < 10 and y > 0:
print("x:", x, "| y:", y)
x = x + 2
y = y - 10
x: 0 | y: 50
x: 2 | y: 40
x: 4 | y: 30
x: 6 | y: 20
x: 8 | y: 10
Both conditions must be True at the same time for the loop to continue. Once x reaches 10, the first condition fails and the loop stops even though y is still greater than 0.
Using or means the loop keeps going as long as at least one condition is True:
a = 1
b = 20
while a < 5 or b > 10:
print("a:", a, "| b:", b)
a = a + 1
b = b - 5
a: 1 | b: 20
a: 2 | b: 15
a: 3 | b: 10
a: 4 | b: 5
The loop stops when both conditions are False simultaneously. When a reaches 5 and b reaches 5 at the same time, neither condition is True and the while loop exits.
A Python while loop is not limited to working with numbers. You can use it to move through the characters of a string one at a time by tracking the current index position.
word = "Python"
index = 0
while index < len(word):
print("Character at position", index, ":", word[index])
index = index + 1
Character at position 0 : P
Character at position 1 : y
Character at position 2 : t
Character at position 3 : h
Character at position 4 : o
Character at position 5 : n
The condition index < len(word) checks that the index has not gone past the last character. On each pass, the character at the current index prints and the index increases by 1. See the Python string docs for everything you can do with strings.
You can also use a while loop to process items from a list by position:
fruits = ["apple", "mango", "banana", "grape", "kiwi"]
position = 0
while position < len(fruits):
print(position + 1, ".", fruits[position])
position = position + 1
1 . apple
2 . mango
3 . banana
4 . grape
5 . kiwi
The while loop walks through the list from index 0 to the last index. The condition position < len(fruits) ensures the loop stops before going out of bounds.
This complete example uses a Python while loop to process a list of student scores. It calculates the total, finds the highest and lowest scores, and prints a summary — all using a single while loop without any built-in functions for the calculations.
scores = [78, 92, 55, 88, 73, 96, 61, 84]
index = 0
total = 0
highest = scores[0]
lowest = scores[0]
while index < len(scores):
score = scores[index]
total = total + score
if score > highest:
highest = score
if score < lowest:
lowest = score
index = index + 1
average = total / len(scores)
print("Scores processed:", len(scores))
print("Total:", total)
print("Average:", average)
print("Highest score:", highest)
print("Lowest score:", lowest)
Scores processed: 8
Total: 627
Average: 78.375
Highest score: 96
Lowest score: 55
The while loop starts at index 0 and moves through every score. On each iteration it adds the score to total and checks whether it is a new highest or lowest. When the index reaches the length of the list, the condition becomes False and the loop ends. The summary statistics are then printed from the accumulated variables.