Python Forum

Full Version: What does the below line mean?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
for _ in range(10):
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
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 :-)
Thank you. I understood it.