You are viewing a single comment's thread. Return to all comments →
_ is used to indicate that you don't care about the value of the variable, and are just using as a placeholder.
"_" is a valid variable name.
So for example if we need an instruction to be executed thrice, one way of writing would be '''
total = 0 for i in range(3): total += 1
'''
but since we're not using variable 'i' inside the loop, value of 'i' doesn't matter
hence same could be written as,
total =0 for _ in range(3): total += 1
p.s.
even better way would be
total = 0 for _ in iter(range(3)): total += 1
But that's a different story :P, I hope this helps ! :D
Seems like cookies are disabled on this browser, please enable them to open this website
List Comprehensions
You are viewing a single comment's thread. Return to all comments →
_ is used to indicate that you don't care about the value of the variable, and are just using as a placeholder.
"_" is a valid variable name.
So for example if we need an instruction to be executed thrice, one way of writing would be '''
'''
but since we're not using variable 'i' inside the loop, value of 'i' doesn't matter
hence same could be written as,
'''
'''
p.s.
even better way would be
'''
'''
But that's a different story :P, I hope this helps ! :D