What are some common conventions for Python?

Here are some common conventions for writing readable Python code:

• Use 4 spaces for indentation, not tabs. This is the standard Python convention.

• Class names should use CapWords (CamelCase). Example: class Dog

• Variable names should use underscore_separated_names. Example: favorite_food

• Function names should also use underscore_separated_names. Example: def print_info()

• Use meaningful and descriptive variable names. Avoid short, generic names like x and y.

• Use upper case for constants. Example: MAX_ITERATIONS = 100

• Follow PEP8 style guidelines in general, including:

  • Maximum 79 characters per line

  • Surround top-level function and class definitions with two blank lines

  • Use 4 spaces for indentation, not tabs

  • Use single quotes around strings, not double quotes

• Use docstrings to document functions and classes. Follow the standard docstring format:

python

Copy

def function():  
    """Function's description."""
    code

• Avoid using from module import *. Only import specific functions.

• Import modules at the top of the file, after any documentation headers or comments.