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缩进代码