Hướng dẫn break function python
In this article, you will learn to use break and continue statements to alter the flow of a loop. Show
Video: Python break and continue Statement
What is the use of break and continue in Python?In Python, Loops iterate over a block of code until the test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without checking test expression. The Python break statementThe If the Syntax of breakbreak Flowchart of breakFlowchart of break statement in PythonThe working of break statement in for loop and while loop is shown below. Working of the break statementExample: Python break
Output s t r The end In this program, we iterate through the "string" sequence. We check if the letter is i, upon which we break from the loop. Hence, we see in our output that all the letters up till i gets printed. After that, the loop terminates. Python continue statementThe Syntax of Continuecontinue Flowchart of continueFlowchart of continue statement in PythonThe working of the Example: Python continue
Output s t r n g The end This program is same as the above example except the We continue with the loop, if the string is i, not executing the rest of the block. Hence, we see in our output that all the letters except i gets printed. Table of Contents
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 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 |