您的位置:首页 > 编程语言 > Python开发

python-return,break,continue,pass,exit( )用法

2020-02-17 12:09 447 查看

(1)return

return 语句就是将结果返回到调用的地方,并把程序的控制权一起返回。程序运行到所遇到的第一个return即返回(退出def块),不会再运行第二个return

[code]>>> def  config():
...     a=4
...     b=3
...     if a>b:
...         return 0
...     a=b
...     return a
...
>>> config()
0

(2)break

break语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归完,也会停止执行循环语句。

break语句用在while和for循环中。如果您使用嵌套循环,break语句将停止执行最深层的循环,并开始执行下一行代码。

[code]>>> def  config():
...     for letter in 'Python':  # 第一个实例
...         if letter == 'h':
...             break
...         print('当前字母 :', letter)
...     print('*****')
...
>>> config()
当前字母 : P
当前字母 : y
当前字母 : t
*****

(3)continue

continue 语句跳出本次循环,而break跳出整个循环。continue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮循环。

continue语句用在while和for循环中。

[code]>>> def  config():
...     for letter in 'Python':  # 第一个实例
...         if letter == 'h':
...           continue
...         print('当前字母 :', letter)
...     print('*****')
>>> config()
当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : o
当前字母 : n
*****

(4)pass

空语句,是为了保持程序结构的完整性。pass 不做任何事情,一般用做占位语句。

(5)exit

用来结束整个程序

[code]>>> def  config( ):
...     for letter in 'Python':  # 第一个实例
...         if letter == 'h':
...             exit()
...         print('当前字母 :', letter)
...     print('*****')
>>> config()
当前字母 : P
当前字母 : y
当前字母 : t

 

  • 点赞
  • 收藏
  • 分享
  • 文章举报
AI新选手 发布了2 篇原创文章 · 获赞 0 · 访问量 118 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: