Python Forum
What does the below line mean?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What does the below line mean?
#1
for _ in range(10):
Reply
#2
single underscore is valid name in python. By convention it is used as throw-away name, i.e. it indicates you are not interested in it and no intention to use it. In the example it would mean you just need to repeat the loop body code 10 times, but counter (i.e. the underscore) is not used at all. Of course you can use other name instead if you prefer.

If you are in interactive mode the underscore name holds the last evaluated result, e.g.
>>> 1 + 1
2
>>> _
2
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
By convention _ symbolises throw-away values/objects.

In this for-loop it signifies that number of particular iteration of for-loop is not important; important is that loop runs 10 times.

EDIT: ninjad by buran :-)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Thank you. I understood it.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020