Detailed Explanation of Usage Examples of else break and continue in Python Cycle
- 2021-07-13 05:48:43
- OfStack
This article illustrates the use of else, break, and continue in the Python loop. Share it for your reference, as follows:
I saw it when I looked at the documentation of Python
for
And
while
Statement is one of the biggest differences from C language-you can have one optional
else
Statement. The execution trigger mechanism of this statement is not clear to me, and it is only through code testing that I know under what circumstances it triggers
else
Statement. "Every existence is reasonable." The designer of Python must have his purpose. Now let's explore it once.
Taking for statement as an example, the development environment Python3.4.
Effect of else in Python Cycle
In a loop
else
The existence of is to make the code clearer and more concise. Here's an example to show you, using traditional writing and tape respectively
else
The writing method of realizes the code with the same function
for Loop Writing of Traditional C Format
myList = [1,2,3,4,5,6,7]
isFound = False
for item in myList:
if item == 4:
isFound = True
print('List Have 4')
break
if not isFound:
print('List No 4')
Belt
else
Statement
for
Circular writing
myList = [1,2,3,4,5,6,7]
for item in myList:
if item == 4:
isFound = True
print('List Have 4')
break
else:
print('List No 4')
Comparing the two codes, it is not difficult to find that compared with the traditional
for
Loop writing, with
else
The statement is written more concisely, and the isFound variable and the if judgment statement after jumping out of the loop are missing.
Summarize
Under the structure of for … else
There is no difference between the statements in for and ordinary (for statement without else); Statements in else are executed after the loop executes normally; else is not executed when the statement in for is interrupted by break jumping out.
Finally, it summarizes a sentence, for... else structure 1 and
while
0
1 use, in order to reflect the strength of this structure (at least I think so, I don't know what other situations, I hope netizens will not hesitate to comment).
while... else structure also meets the above conditions.
continue and break in Python Cycle
continue
And
while
0
Statement is actually the same as the usage of C language, only in for... else structure
for
The statement in passes the
while
0
When jumping out and interrupting, it will not be executed again
else
The contents in;
And will
continue
Statement is no different from ordinary statement, as long as it is not failed
while
0
, light has
continue
Will enter
else
Statement.
continue Code Sample
The code is as follows:
for x in range(1, 4):
print(x, 'for Statement ')
continue
print(x, 'continue After statement ')
else:
print(x, 'else Statement ')
Implementation results:
1 for statement
2 for statement
3 for statement
3 else statement
break Code Sample
The code is as follows:
for x in range(1, 4):
print(x, 'for Statement ')
break
print(x, 'break After statement ')
else:
print(x, 'else Statement ')
Implementation results:
1 for statement
For more readers interested in Python related contents, please check the topics of this site: "Introduction and Advanced Classic Tutorial of Python", "Data Structure and Algorithm Tutorial of Python", "Summary of Function Use Skills of Python", "Summary of String Operation Skills of Python" and "Summary of File and Directory Operation Skills of Python"
I hope this article is helpful to everyone's Python programming.