LoginSignup
3
3

More than 5 years have passed since last update.

pythonにおける“forループ”構文の注意点(~for i in range()~)

Posted at

Pythonを学習している中で、陥ったミスをまとめています。
初心者向けですので、あたりまえだと思う方はスキップしてください。

windowsにおけるコマンド プロンプトやmacのターミナルにおいて、

import numpy as np
a = np.arange(10)
for i in range(10):
   print(a[i])

とすると

0
1
2
3



9

と表示されます。また、

import numpy as np
a = np.arange(10)
for i in range(1,10):
   print(a[i])

とすると

1
2
3



9

と表示されます。その理由として、

for i in range(10):

では、

i = 0, 1, 2, 3, ・・・ ,9

と推移していくのに対して、

for i in range(1,10):

では

i = 1, 2, 3, ・・・ ,9

で推移しており、

i = 0

がないことがわかります。

3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3