python:for_range_enumerate

for range enumerate 文

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
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

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

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
  • python/for_range_enumerate.txt
  • 最終更新: 2019/08/19 06:52
  • by ともやん