What are the two types of while loops?
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 Can you have 2 while loops?Can you have 2 while loops? A nested while loop is a while statement inside another while statement. In a nested while loop, one iteration of the outer loop is first executed, after which the inner loop is executed.
What type is while loop?A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.
What are loops and its types?Types of Loops
A for loop is a loop that runs for a preset number of times. A while loop is a loop that is repeated as long as an expression is true. An expression is a statement that has a value. A do while loop or repeat until loop repeats until an expression becomes false.
What are the two types of loops in Python?The three types of loops in Python programming are: while loop. for loop. nested loops.
|