Python学习day7和day8(for语句,else语句)

for循环

语法:

for 临时变量 in 序列:
    代码1
    代码2

序列:数据序列,字符串(hello world),每一个字符就是数据,连一起就是数据序列

str1='zzcsdbd'
for i in str1:
    print(f'{i}',end='')
print( )

break

str1='zzcsdbd'
for i in str1:
    if i=='s':
        break
    print(f'{i}',end='')

输出:zzc

continue

str1='zzcsdbd'
for i in str1:
    if i=='s':
        continue
    print(f'{i}',end='')

输出:zzcdbd

和while一样一样的

else语句

while~else语句

while 条件:
    代码1
else
    循环正常结束之后要执行的代码
i=1
while i<5:
    print('hhh',end='')
    i+=1
else:
    print('ooo',end='')

输出hhhhhhhhhhhooo

for~else语句

str1='zzcjiayou'
for i in str1:
    print(i,end='')

else:
    print('ooo',end='')

结果:zzcjiayouooo

注意:如果while/for循环加的是continue那么else的语句在循环结束后输出,如果是break,那么else不会执行,因为continue是退出了一次循环,继续下一循环,所以该循环在continue控制下是可以正常结束的,当循环结束以后,则执行了else缩进代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值