LoginSignup
17
17

More than 1 year has passed since last update.

pythonの「for _ in range():」について

Last updated at Posted at 2020-05-05

**「for _ in range():」**について備忘録。

pythonのfor文と言えば

sample1.py
for `変数` in `オブジェクト`:
    #処理内容

で、変数には「i」,「j」,「k」,「loop」,「iter」などを使うと思います。
一方で、for文ではfor文の内側での処理内容にloop変数を使用しない場合があります。
その時に、for文の変数名に**「_(アンダーバー/アンダースコア, 正式名称はアンダーバー)」**を使うことで、変数名を無駄に使用することなくfor文によるloopが可能です。

sample2.py
for _ in `オブジェクト`:
    #処理内容

例えばこんな感じ。
コード

sample3.py
for _ in range(5):
  print("Atcoder")

出力

output3
Atcoder
Atcoder
Atcoder
Atcoder
Atcoder

loopで繰り返し変数使わないのに指定するの気持ち悪いなあと思っていたので、今後はこれを使おうと思います。

17
17
2

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