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

(1)return

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

>>> 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语句将停止执行最深层的循环,并开始执行下一行代码。

>>> 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循环中。

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

(4)pass

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

(5)exit

用来结束整个程序

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

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值