python学习笔记7-流程控制语句

# 1.if
x = 1
if x < 0:
    print('负数')
elif x == 0:
    print('')
else :
    print('正数')
# 正数

# 2.for
total = 0
L = [1,2,3,4]
for y in L:
    total += y #等价于total = total + y
total
# 10

# 3.while
m,n = 0,1
while m<10:
    print(m)
    m,n = n,m+n
# 0
# 1
# 1
# 2
# 3
# 5
# 8
# 通过while循环输出斐波那契序列

# 4.break
 # break用于跳出最近的for或while循环
for i in range(3):
    for j in range(3):
        if j>i:
            break
        print((i,j))
# (0, 0)
# (1, 0)
# (1, 1)
# (2, 0)
# (2, 1)
# (2, 2)

# 5.continue
# continue用于跳过当前循环,继续循环中的下一次迭代
L =[1,2,None,4,5]
total = 0
for x in L:
    if x is None:
        continue
    total += x
total
# 12
# 循环时跳过了None

# 6.pass
# pass语句什么也不做,当语法上需要一个语句,但程序需要什么动作也不做时使用

猜你喜欢

转载自www.cnblogs.com/babysteps/p/python_note_7.html
今日推荐