Python巩固基础03-循环语句


Python中循环语句即为:当满足某一条件时,重复执行其中的代码块(缩进的代码块),直到不再满足该条件为止;
Python分为while循环和for循环。

while循环

基本格式:

# else为可选
while condition:
    statements1
else:
    statements2

解释:当condition条件为True时--执行statements1--再次判断condition条件是否为True--满足则执行statements1--循环判断condition条件是否为True,直到为False则退出循环,执行接下来的代码或者else中的代码。其中else为可选,意为若condition为False,则执行else中的代码。
注意:在while循环中,需要设置退出循环的条件,否则会死循环(需要死循环的场景除外)。

例子1:

x = 1
while x < 5:
    print(x)
    x += 1

输出结果

1
2
3
4

当x<5时,循环打印出x,每打印一次则x自增一次,直到x不再小于5时,退出while循环。

例子2:

x = 1
while x < 5:
    print(x)
    x += 1
else:
    print('done')

输出结果

1
2
3
4
done

当x为4时,打印出x,x再自增1即为5,不再满足x < 5的条件,则执行else中的代码块。

例子3:

x = 5
while x < 5:
    print(x)
    x += 1
else:
    print('x不小于5')

输出结果

x不小于5

x初始值为5,不满足x < 5的条件,直接执行else中的代码块。

for循环

for循环用于遍历可迭代序列。
基本格式:

# else为可选
for  in :
    statements1
else:
    statements2

解释:遍历序列中的元素,每遍历一次执行一次statements1;指遍历的序列中的元素,变量,自定义、为遍历的可迭代序列,当不使用或者无意义时,可以使用_来代替,作占位符用途;当序列中的元素遍历完成后,执行else中的statements2。

例子1:

for x in 'hello':
    print('x is {}'.format(x))

输出结果

x is h
x is e
x is l
x is l
x is o

遍历'hello',输出每个元素。

例子2:

for x in 'hello':
    print('x is {}'.format(x))
else:
    print('done')

输出结果

x is h
x is e
x is l
x is l
x is o
done

遍历'hello',输出每个元素,直到遍历结束,则输出done。

循环中的breakcontinue

break退出循环
单层循环的break
例子1(while):

x = 1
while x < 10:
    if x == 8:
        x += 1
        break
    if x % 2 == 0:
        print(x)
    x += 1

输出结果

2
4
6

x初始值为1,当x小于10时,循环输出为偶数的x,每循环一次x自增1;当x为8时,退出循环。

例子2(for):

for x in range(10):
    if x == 8:
        break
    print('x is {}'.format(x))

输出结果

x is 0
x is 1
x is 2
x is 3
x is 4
x is 5
x is 6
x is 7

输出从0到9的整数,当x为8时,退出循环。
备注:range(n)生成从0到n-1的数列,range(x, n)生成从x到n-1的数列。

多层循环中的break
例子1(while):

x = 0
while x < 3:
    y = 0
    while y < 5:
        if y == 2:
            y += 1
            break
        print(y)
        y += 1
    x += 1

输出结果

0
1
0
1
0
1

第一层循环:x初始值为0,当x<3时,进入循环,每循环一次x自增1;
第二层循环:y初始值为0,当y<5时,进入循环,若y不为2,则打印y,每循环一次y自增1。
第一层循环每循环一次,都会进行一次第二层的整个while循环,当第二层循环中y为2时,则退出整个第二层循环,直接进行下一次的第一层循环。即-break只退出本层循环。

例子2(for):

for x in ['hello', 'world', 'test']:
    for y in ['hello', 'world', 'test']:
        if y == 'world':
            break
        print(x+y)

输出结果

hellohello
worldhello
testhello

在第二层for循环中,若y为'world',则退出本次for循环,继续下次第一层for循环。

continue退出本次循环,继续下次循环
单层循环的continue
例子1(while):

x = 1
while x < 10:
    if x == 4:
        x += 1
        continue
    if x % 2 == 0:
        print(x)
    x += 1

输出结果

2
6
8

x初始值为1,当x小于10时,循环输出为偶数的x,每循环一次x自增1;当x为4时,不输出,跳出本次while循环,继续下次while循环。

例子2(for):

for x in range(5):
    if x == 3:
        continue
    print('x is {}'.format(x))

输出结果

x is 0
x is 1
x is 2
x is 4

输出从0到4的整数,当x为3时,跳出本次for循环,继续进行下一次for循环。

多层循环中的continue
例子1(while):

x = 0
while x < 3:
    y = 0
    while y < 5:
        if y == 2:
            y += 1
            continue
        print(y)
        y += 1
    x += 1

输出结果

0
1
3
4
0
1
3
4
0
1
3
4

在第二层循环中,若y不为2,则打印y,若y为2,则跳出本次循环,继续第二层循环中的下一次循环。

例子2(for):

for x in ['hello', 'world', 'test']:
    for y in ['hello', 'world', 'test']:
        if y == 'world':
            continue
        print(x+y)

输出结果

hellohello
hellotest
worldhello
worldtest
testhello
testtest

在第二层for循环中,若y为'world',则跳出本次for循环,继续下次第二层for循环。