This text is part of a Real Python tutorial by Leodanis Pozo Ramos.
Python’s while loop enables you to execute a block of code repeatedly as long as a given condition remains true. A while loop is ideal for situations where the number of iterations isn’t known upfront.
Loops are a pretty useful construct in Python, so learning how to write and use them is a great skill for you as a Python developer.
By the end of this lesson, you’ll understand that:
whileis a Python keyword used to initiate a loop that repeats a block of code as long as a condition is true.- A
whileloop works by evaluating a condition at the start of each iteration. If the condition is true, then the loop executes. Otherwise, it terminates. whileloops are useful when the number of iterations is unknown, such as waiting for a condition to change or continuously processing user input.while Truein Python creates an infinite loop that continues until abreakstatement or external interruption occurs.
With this knowledge, you’re prepared to write effective while loops in your Python programs, handling a wide range of iteration needs.
Getting Started With Python while Loops
In programming, loops are control flow statements that allow you to repeat a given set of operations a number of times. In practice, you’ll find two main types of loops:
forloops are mostly used to iterate a known number of times, which is common when you’re processing data collections with a specific number of data items. You’ll learn aboutforloops later in this course.whileloops are commonly used to iterate an unknown number of times, which is useful when the number of iterations depends on a given condition.
Python while loops are compound statements with a header and a code block that runs until a given condition becomes false. The basic syntax of a while loop is shown below:
while condition:
<body>
In this syntax, condition is an expression that the loop evaluates for its truth value. If the condition is true, then the loop body runs. Otherwise, the loop terminates. Note that the loop body can consist of one or more statements that must be indented properly.
Here’s a more detailed breakdown of this syntax:
whileis the keyword that initiates the loop header.conditionis an expression evaluated for truthiness that defines the exit condition.<body>consists of one or more statements to execute in each iteration.
Here’s a quick example of how you can use a while loop to iterate over a decreasing sequence of numbers:
>>> number = 5
>>> while number > 0:
... print(number)
... number -= 1
...
5
4
3
2
1
In this example, number > 0 is the loop condition. If this condition returns a false value, the loop terminates. The body consists of a call to print() that displays the value on the screen. Next, you decrease the value of number. This change will produce a different result when the loop evaluates the condition in the next iteration.
The loop runs while the condition remains true. When the condition turns false, the loop terminates, and the program execution proceeds to the first statement after the loop body. In this example, the loop terminates when number reaches a value less than or equal to 0.
If the loop condition doesn’t become false, then you have a potentially infinite loop. Consider the following loop, and keep your fingers near the Ctrl+C key combination to terminate its execution: