Python 循环控制保留字(break,continue)

for i in "Python":
    if i =="y":
        continue
    print(i,end="")

输出:Pthon

跳出if条件成立

for i in "python":
    if i == "t":
        break
    print(i,end="")

输出:py

结束循环

s = "Python"
while s != "":
    for i in s:
        print(i,end="")
    s = s[:-1]

输出:PythonPythoPythPytPyP

s = "Python"
while s != "":
    for i in s:
        if i =="t":
            break
        print(i,end="")
    s = s[:-1]

输出:PyPyPyPyPyP

 

循环与else

Python 循环控制保留字(break,continue)_第1张图片

s = 'python'
for i in s:
    if i == 't':
        continue
    print(c,end="")
else:
    print("正常退出")

输出:pyhon正常退出

s = 'python'
for i in s:
    if i == 't':
        break
    print(i,end="")
else:
    print("正常退出")

输出:py

你可能感兴趣的:(Python)