Getting Started with Python's flow control statements
1, if determination
(1) if single
if - if
if 条件:
缩进 结果
(The official recommended four spaces, or a tab space and tab can not be mixed)
money = 10
print("从学校出发")
if money >= 10:
print("买个炸鸡")
print("买个啤酒")
print("走啊走")
print("到家了")
(2) if else - - Select either:
if 条件:
缩进 结果
else:
缩进 结果
if 3>2:
print("这是如果执行了")
print("123")
print("234")
else:
print("这是否则执行了")
(3) if elif elif elif --- a multiple-choice or not vote
if 条件:
缩进 结果
elif 条件:
缩进 结果
elif 条件:
缩进 结果
if 3>2:
print("这是A")
elif 4>3:
print("这是B")
elif 5>3:
print("这是C")
elif 6>3:
print("这是D")
(4) if elif elif else - election of a
if 条件:
缩进 结果
elif 条件:
缩进 结果
elif 条件:
缩进 结果
else 条件:
缩进 结果
if 1>2:
print("A")
elif 2>3:
print("B")
elif 5>6:
print("c")
else:
print("D")
(5) if if if - a plurality of conditions selected from a plurality of
if 条件:
缩进 结果
if 条件:
缩进 结果
if 条件:
缩进 结果
if 3>2:
print("A")
if 4>2:
print("B")
if 6>3:
print("C")
(6) if nesting:
if 条件:
if 条件:
缩进 结果
else 条件:
缩进 结果
else 条件:
缩进 结果
sex = "女"
age = 30
if sex == "女":
if age == 30:
print("进来坐一坐")
else:
print("隔壁找太白")
else:
print("滚犊子")
2, while loop:
while loop: death cycle (by circulating condition can be terminated and break)
(1) while --- Keyword
while 条件:
循环体
while True:
print("爱情买卖")
print("痒")
print("年少有为")
print("浮夸")
print("威风堂堂")
print("大悲咒")
print("情锁")
(2) break terminates the current cycle:
while True:
循环体
Break
语句
while True:
print("爱情买卖")
break
break ---- must be used in a loop
terminates the current ---- break through the bad and the code below the break will not be executed
(3) Continue out this cycle, Continue (masquerading as the last line of the loop body) for the next cycle
while True:
循环体
Continue
语句
Continue ---- out this cycle, continue to the next cycle (loop disguised last line of code)
---- continue to be used in a loop, and the code will not be executed continue downward
while True:
print("爱情买卖")
print("痒")
continue
print("年少有为")
(4) while else --- a whole cycle
while True:
循环体
else:
语句
while else --- after while when the conditions are not true, execute the statement after else
flag = True
while flag:
print(123)
flag = False
else:
print("循环结束!")
当while循环体中出现了break就不会再执行else
Exercise:
使用while输出10 - 57的数字(包含10和57)
num = 10
while num <= 57:
print(num)
num = num + 1
使用while 输出 100-10 的数字(包含100和10)
num = 100
while num > 9:
print(num)
num = num - 1
3, for loop:
for i in variable:
Execute the statement
msg = "好好学习,天天向上"
for a in msg:
print(a)
print(a)
pass: too, occupying
for i in variable:
pass ... and pass the same function, but it is recommended to use pass
for a in "abcds":
pass # 过 占位
print(a)
Execute the statement
pass is equivalent to the implementation of a do-nothing statement in a for loop
for - I --- key variable names in ---- keywords msg --- iterables
(Iterables: in Python data type, in addition to int, bool can rest iteration)
The for loop is cyclic data structure:
String (str)
List (list)
Tuples (tuple)
Dictionary (dict)
Collection (set)
Alone can not circulate is integer (int) and boolean value (bool)
for circulation at the time of the cycle has been assigned