__name__: Used to implement special behaviour, such as the+operator for classes with the__add__method. More info_name: Indicates that a variable is "private" and should only be used by the class or module that defines itname_: Used to avoid naming conflicts. For example, asclassis a keyword, you could call a variableclass_instead__name: Causes the name to be "mangled" if defined inside a class. More info
A single underscore, _, has multiple uses:
- To indicate an unused variable, e.g. in a for loop if you don't care which iteration you are on
for _ in range(10):
print("Hello World")
- In the REPL, where the previous result is assigned to the variable _
>>> 1 + 1 # Evaluated and stored in `_`
2
>>> _ + 3 # Take the previous result and add 3
5
- In integer literals, e.g. x = 1_500_000 can be written instead of x = 1500000 to improve readability
See also "Reserved classes of identifiers" in the Python docs, and this more detailed guide.