Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Mastering While Loops Show Iteration means executing the same block of code over and over, potentially many times. A programming structure that implements iteration is called a loop. In programming, there are two types of iteration, indefinite and definite:
In this tutorial, you’ll:
When you’re finished, you should have a good grasp of how to use indefinite iteration in Python. The while LoopLet’s see how Python’s The format of a rudimentary
The controlling expression, When a Consider this loop: >>>
Here’s what’s happening in this example:
Note that the controlling expression of the >>>
In the example above, when the loop is encountered, Here’s another >>>
When a list is evaluated in Boolean context, it is truthy if it has elements in
it and falsy if it is empty. In this example, The Python break and continue StatementsIn each example
you have seen so far, the entire body of the
The distinction between Here’s a script file called
Running
When The next script,
The output of
This
time, when The else ClausePython allows an
optional
The About now, you may be thinking, “How is that useful?” You could accomplish the same thing by putting those statements immediately after the
What’s the difference? In the latter case, without the When Consider the following example: >>>
In this case, the loop repeated until the condition was exhausted: >>>
This loop is terminated prematurely with It may seem as if the meaning of the word One of the following interpretations might help to make it more intuitive:
If you don’t find either of these interpretations helpful, then feel free to ignore them. When might an >>>
An Infinite LoopsSuppose you write a Consider this example: >>>
This code was terminated by Ctrl+C, which generates an interrupt
from the keyboard. Otherwise, it would have gone on unendingly. Many Clearly, Maybe that doesn’t sound like something you’d want to do, but this pattern is actually quite common. For example, you might write code for a service that starts up and runs forever accepting service requests. “Forever” in this context means until you shut it down, or until the heat death of the universe, whichever comes first. More prosaically, remember that loops can be broken out of with the Here’s another variant of the loop shown above that successively removes items from a
list using >>>
When You can also specify multiple
In cases like this, where there are multiple reasons to end the loop, it is often cleaner to Infinite loops can be very useful. Just remember that you must ensure the loop gets broken out of at some point, so it doesn’t truly become infinite. Nested while LoopsIn general, Python control structures
can be nested within one another. For example,
Similarly, a >>>
A
Additionally,
In fact, all the Python control structures can be intermingled with one another to whatever extent you need. That is as it should be. Imagine how frustrating it would be if there were unexpected restrictions like “A Seemingly arbitrary numeric or logical limitations are considered a sign of poor program language design. Happily, you won’t find many in Python. One-Line while LoopsAs with an >>>
This only works with simple statements though. You can’t combine two compound statements into one line. Thus, you can specify a >>>
But you can’t do this: >>>
Remember that PEP 8 discourages multiple statements on one line. So you probably shouldn’t be doing any of this very often anyhow. ConclusionIn this
tutorial, you learned about indefinite iteration using the Python
You should now have a good grasp of how to execute a piece of code repetitively. The next tutorial in this series covers definite iteration with
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Mastering While Loops What is the condition of a while loop?A "While" Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. For example, if we want to ask a user for a number between 1 and 10, we don't know how many times the user may enter a larger number, so we keep asking "while the number is not between 1 and 10".
Is while a loop or a condition statement?The While loop and the For loop are the two most common types of conditional loops in most programming languages.
Can we write and condition in while loop?You initiate the loop with the while keyword, then set the condition to be any conditional expression. A conditional expression is a statement that evaluates to True or False . As long as the condition is true, the loop body (the indented block that follows) will execute any statements it contains.
|