欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Python | 展示一个break语句示例

发布时间:2023/12/1 56 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Python | 展示一个break语句示例 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

break is a keyword in python just like another programming language and it is used to break the execution of loop statement.

就像另一种编程语言一样, break是python中的关键字,用于中断loop语句的执行。

In the given example, loop is running from 1 to 10 and we are using the break statement if the value of i is 6. Thus when the value of i will be 6, program's execution will come out from the loop.

在给定的示例中,循环从1到10运行,并且如果i的值为6,则使用break语句。因此,当i的值为6时,程序的执行将从循环中执行。

Example 1:

范例1:

for i in range(1,11):if(i==6):breakprint(i)

Output

输出量

1 2 3 4 5

Example 2: In this example, we are printing character by character of the value/string “Hello world” and terminating (using break), if the character is space.

示例2:在此示例中,如果字符为空格,则按字符/值/字符串“ Hello world”打印字符并终止(使用break)。

for ch in "Hello world":if ch == " ":breakprint(ch)

Output

输出量

H e l l o

翻译自: https://www.includehelp.com/python/break-statement-example.aspx

总结

以上是生活随笔为你收集整理的Python | 展示一个break语句示例的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。