Python入門(5) 繰り返し文
プログラミングにおいて、なんども同様の処理を繰り返すfor文またはwhile文が必要になる。 while文があればfor文は不要であるが、for文は多用される。
for文: 決まった回数だけ繰り返す
ブロック指定されたプログラム単位をキーワード for を使ったfor文で、指定した回数だけ繰り返すことができる。
次のPython構文は、range 関数で生成したリスト [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] から先頭から要素を1つずつ取り出しながらカウター変数 i に代入しながら、文Aから文Zまでを10回繰り返す。 for のある行の末尾はコロン(:)で改行することに注意する。for i in range(1, 11): 文A .... 文Z
次のスクリプト for.py を見てみよう。 2つの点に注意して欲しい。
- for のある行には末尾にコロン(:)があり改行して、次行は字下げしている
- 繰り返される複(数)文はそれぞれ1行ずつ書いて、予約語 for に比べて同じ字数だけ字下げ(indent)して記述する(ブロック文)。
for i in range(1, 11): print('value of i :', i, end = '') print('\tcaliculating i ** 3: ', i ** 3) print('Here is out of for block')
実行結果は次のようになる。
value of i : 1 caliculating i ** 3: 1 value of i : 2 caliculating i ** 3: 8 value of i : 3 caliculating i ** 3: 27 value of i : 4 caliculating i ** 3: 64 value of i : 5 caliculating i ** 3: 125 value of i : 6 caliculating i ** 3: 216 value of i : 7 caliculating i ** 3: 343 value of i : 8 caliculating i ** 3: 512 value of i : 9 caliculating i ** 3: 729 value of i : 10 caliculating i ** 3: 1000 This is out of for block
Python shellにrange関数が生成するリストを表示させてみよう。
>>> range(1,11) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> range(-3,7) [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6]
range関数 range(m, n) は $m < n$ であれば負の数であってもよく、$m$ から始まる$n - m$個の要素からリストを返すことがわかる。
iterable要素をカウンタ値とする

Pythonでは、for文のカウンタ変数の取る値は上の例のように整数だけでなく、リスト記号 [ ] で挟まれたリスト要素なら何でもよい。
このことは、既にを付けたPython入門(3) 文字列操作/文字列の一部を切り出すで紹介しているfor文で使っている。
このことはiterable要素をカウンタ値とするfor文でも触れる。
Pythonの繰り返し for a in B: であらわれる B は繰り返し可能オブジェクト(iterable)である。 iterableなオブジェクトとは要素が並んでいるデータで、順番に次の要素が取り出されるというようになっているデータである。 以下の例のように、文字列が並んでいるリストや、文字(letter)の並びである文字列(string)はiterableであり、for文で使うことができるということだ。
次のスクリプトを見てみよう。prev = "" for animal in ['cat', 'dog', 'tiger', 'lion', 'puma', 'horse']: print('value of animal :', animal) prev = prev + animal print(prev) print('Here is out of for block')
for文の入れ子(nest)
for文など多くの構文は入れ子にすることができる。 次のスクリプトは、Pythonからみると、1つのfor文(そのカウンタ変数が i である)である。 ただし、この外側のfor文で繰り返されるブロックは print('outside for')とfor文(そのカウンタ値 が k)の2つの文から構成されてい。
for i in range(1, 5): print('outside k-for') for k in range(i): print('inside k-for: i = ', i, ' k = ', k)
while文:条件が満たされいる限り繰り返す
予約語 while に続いて条件式の値が True である限り、ブロックを繰り返し実行するのがキーワード while文である。
while 条件式: 文1 .... 文n
次の2つの点に注意して欲しい。
- キーワード while の行末にはコロン(:)があって改行され、次行は必ず字下げされる
- 条件式が True である間に繰り返されるブロック文は、while に比べて同じ字数だけ字下げ(indent)して記述する(ブロック文)。
次のスクリプト while.py は、キーボードから文字 'e' または 'E' を入力しない限り、i が 1、2,3, ..と1つづ増えて、その3乗を計算し続ける(計算し続けるためにはリターンキーを押し続けてもよい)。
i = 1 letters = input('repeat more? (e/E): ') while not(letters == 'e' or letters == 'E'): print('i = ', i, ':', end = '') print('\ti ** 3 = ' , i ** 3) i = i + 1 letters = input('repeat more? (e/E): ') print("'while loop' has just terminated")
重要: for文をwhile文に書き換える
数値カウンター k を使った次のfor文は常にwhile文に書き換えることができる。
for k in range[m, n]: 文1 ... 文n$\qquad\displaystyle\Downarrow$ 上のfor文は常に次のwhile文に書き換えることができる
k = m while k < n: 文1 ... 文n k = k + 1
iterateration number(>0) = 4 i = 1 : i ** 3 = 1 i = 2 : i ** 3 = 8 i = 3 : i ** 3 = 27 i = 4 : i ** 3 = 64 This is out of for block
while文の入れ子(nest)
while文は構文は入れ子にすることができる。
i = 1 k = 5 while (i > 0): print('ourside in-while') while (k < 5): print('inside in-while') print('inside while: i = ', i, ' k = ', k)