How to use break in python

藏色散人
Release: 2019-06-24 10:56:37
Original
3310 people have browsed it

How to use break in python

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
Copy after login

Flow chart:

How to use break in python

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!"
Copy after login

Execution results of the above examples:

当前字母 : P
当前字母 : y
当前字母 : t
当前变量值 : 10
当前变量值 : 9
当前变量值 : 8
当前变量值 : 7
当前变量值 : 6
Good bye!
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!