very-cool-group

quotes

Single and Double quoted strings are the same in Python. The choice of which one to use is up to you, just make sure that you stick to that choice.

With that said, there are exceptions to this that are more important than consistency. If a single or double quote is needed inside the string, using the opposite quotation is better than using escape characters.

Example:

'My name is "Guido"'   # good
"My name is \"Guido\"" # bad

"Don't go in there"  # good
'Don\'t go in there' # bad
Note: If you need both single and double quotes inside your string, use the version that would result in the least amount of escapes. In the case of a tie, use the quotation you use the most.

References:
- pep-8 on quotes
- convention for triple quoted strings

underscore
  • __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 it
  • name_: Used to avoid naming conflicts. For example, as class is a keyword, you could call a variable class_ 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.