python:for_range_enumerate

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

次のリビジョン
前のリビジョン
python:for_range_enumerate [2016/01/22 16:07] – 作成 ともやんpython:for_range_enumerate [2019/08/19 06:52] (現在) ともやん
行 1: 行 1:
 ====== for range enumerate 文 ====== ====== for range enumerate 文 ======
-range関数による繰り返し+ 
 +===== range 関数による繰り返し =====
 <code python> <code python>
 In : range(5) In : range(5)
行 14: 行 15:
 index: 4 index: 4
 </code> </code>
 +===== enumerate 関数によるリストの繰り返し =====
 +
 +<code python>
 +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
 +</code>
 +
 +===== continue と break =====
 +一定の条件で処理をスキップ
 +<code python>
 +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
 +</code>
 +一定の条件で処理を中止
 +<code python>
 +for index in range(10):
 +    if index >= 4:
 +        break
 +    print('index: {0}'.format(index))
 +     
 +index: 0
 +index: 1
 +index: 2
 +index: 3
 +</code>
 +
  • python/for_range_enumerate.1453446430.txt.gz
  • 最終更新: 2019/05/18 02:23
  • (外部編集)