目次

for range enumerate 文

range 関数による繰り返し

In : range(5)
Out: [0, 1, 2, 3, 4]
 
for index in range(5):
    print('index: {0}'.format(index))
 
index: 0
index: 1
index: 2
index: 3
index: 4

enumerate 関数によるリストの繰り返し

In : colors = ['red', 'blue', 'green', 'pink', 'white']
 
for index, color in enumerate(colors):
    print('color{0}: {1}'.format(index, color))
 
color0: red
color1: blue
color2: green
color3: pink
color4: white

continue と break

一定の条件で処理をスキップ

for index in range(10):
    if index >= 2 and index <= 7:
        continue
    print('index: {0}'.format(index))
 
index: 0
index: 1
index: 8
index: 9

一定の条件で処理を中止

for index in range(10):
    if index >= 4:
        break
    print('index: {0}'.format(index))
 
index: 0
index: 1
index: 2
index: 3