This article mainly introduces the use of break statements in Python, is the introduction of Python exhaled knowledge, the need for friends can refer to the
The break statement in Python terminates the current loop, and continues execution of the next statement, as in the C language.
The most common use of break is when some external conditions are triggered and need to exit from a loop interrupt. Break statements can be used in while and for loops.
If you are using a nested loop (that is, another loop in one loop), the break statement can be used to stop the execution of the inner loop and execute the next line of code in the outer loop.
The syntax for the break statement in Python is as follows:
|1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15||#!/usr/bin/python in ' python ': # i-Example if letter = = ' h ': Break print ' ' = # Second Example while var > 0:print ' current variable value: ', var var = var-1 If var = 5:break print ' Go OD bye! "|
When the above code is executed, the following results are produced:
|1 2 3 4 5 6 7 8 9||Current Letter:p current letter:y current letter:t current variable value:10 current variable value:9 current VA Riable value:8 Current variable value:7 current variable value:6 good bye!|