(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