How to use break in python?
The Python break statement, like in C, breaks the minimally enclosed for or while loop.
The break statement is used to terminate the loop statement. That is, if the loop condition does not have a False condition or the sequence has not been completely recursed, the execution of the loop statement will also be stopped.
The break statement is used in while and for loops.
If you use nested loops, the break statement will stop execution of the deepest loop and start execution of the next line of code.
Python language break statement syntax:
break
Flow chart:
Example (Python 2.0)
#!/usr/bin/python # -*- coding: UTF-8 -*- for letter in 'Python': # 第一个实例 if letter == 'h': break print '当前字母 :', letter var = 10 # 第二个实例 while var > 0: print '当前变量值 :', var var = var -1 if var == 5: # 当变量 var 等于 5 时退出循环 break print "Good bye!"
Execution results of the above examples:
当前字母 : P 当前字母 : y 当前字母 : t 当前变量值 : 10 当前变量值 : 9 当前变量值 : 8 当前变量值 : 7 当前变量值 : 6 Good bye!
Related recommendations: "Python Tutorial"
The above is the detailed content of How to use break in python. For more information, please follow other related articles on the PHP Chinese website!