0

The statement

 for _ in range(10):
         print(_)

gives the output

0
1
2
3
4
5
6
7
8
9

In this case, does the _ work like an variable?

Adarsh D
  • 511
  • 6
  • 14

2 Answers2

1

_ is not an operator, it's just a variable name, there's nothing special about it – except for the REPL, where, unless manually reassigned, it'll be the value of the last expression:

~ $ python3
Python 3.7.6 (default, Dec 30 2019, 19:38:26)
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 5 + 5
10
>>> _ + 10
20
>>> _ + 10
30
>>> _ + 10
40
>>>
AKX
  • 152,115
  • 15
  • 115
  • 172
0

_ is not an operator. It's an identifier that is, by convention, used to indicate that you don't care about the value. So, something like for _ in range(10) would mean that you want to loop 10 times but don't really care about the index (e.g. reattempting a connection etc.)

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169