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语句什么也不做,当语法上需要一个语句,但程序需要什么动作也不做时使用

 

转载于:https://www.cnblogs.com/babysteps/p/python_note_7.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值