Python’s while loop enables you to execute a block of code repeatedly as long as a given condition remains true. Unlike for loops, which iterate a known number of times, while loops are 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 tutorial, 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.- Python lacks a built-in do-while loop, but you can emulate it using a
while Trueloop with abreakstatement for conditional termination.
With this knowledge, you’re prepared to write effective while loops in your Python programs, handling a wide range of iteration needs.
Get Your Code: Click here to download the free sample code that shows you how to work with while loops in Python.
Take the Quiz: Test your knowledge with our interactive “Python while Loops: Repeating Tasks Conditionally” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Python while Loops: Repeating Tasks ConditionallyIn this quiz, you'll test your understanding of Python's while loop. This loop allows you to execute a block of code repeatedly as long as a given condition remains true. Understanding how to use while loops effectively is a crucial skill for any Python developer.
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.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 has both of these loops and in this tutorial, you’ll learn about while loops. In Python, you’ll generally use while loops when you need to repeat a series of tasks an unknown number of times.
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