4.Python-控制流

2019-06-17  本文已影响0人  Yuri7

控制流

if, elif和else

if <condition>: 
    <statesments>
elif <condition>: 
    <statesments>
elif <condition>: 
    <statesments>
...
else:
    <statesments>

while循环

执行<statesments>,直到<condition>不满足为止

while <condition>: 
    <statesments>

range函数

range(start,end, step=1)
range(start,end),step缺省时默认为1
range(end),start缺省时默认为0

for循环

for <value> in <collection>: 
    <indented block of code>

for 循环会遍历完<collection>中所有元素为止

pass空操作

可以用于没有任何功能的代码块中,Python是根据空白符划分代码块的,所以存在是很有必要的。而且pass可以用作代码中的占位符

continue和Break语句

遇到 continue 的时候,程序会返回到循环的最开始重新执行。
遇到break 的时候,程序会跳出循环,不管循环条件是不是满足。

else和break

当循环正常结束时,循环条件不满足, else 被执行;
当循环被 break 结束时,循环条件仍然满足, else 不执行。

上一篇 下一篇

猜你喜欢

热点阅读