5. python学习之for循环

5. python学习之for循环_第1张图片

问题:如何利用for循环,输出以下结果:

1 + 1 = 2
2 + 1 = 3
.
.
10 + 1 = 11
for num in range(1, 11):
    print(str(num) + ' + 1 =', num + 1)

for 和if 嵌套


songslist = ['Holy Diver', 'Thunderstruck', 'Rebel Rebel']
for song in songslist:
    if song == 'Holy Diver':
        print(song, ' - Dio')
    elif song == 'Thunderstruck':
        print(song, ' - AC/DC')
    elif song == 'Rebel Rebel':
        print(song, ' - David Bowie')

for 嵌套循环


5. python学习之for循环_第2张图片
乘法口诀表
for i in range(1, 10):
    for j in range(1, 10):
        print('{} X {} = {}'.format(i, j, i * j))

你可能感兴趣的:(5. python学习之for循环)