Rules for Naming and Using Variable Names (Identifiers) in Python

Rules for Naming and Using Variable Names (Identifiers) in Python

In Python, an identifier is a name given to entities like variables, functions, classes, etc. Identifiers are essential because they enable you to reference and manipulate the data stored in variables or the behavior encapsulated in functions. However, there are specific rules and conventions you must follow when naming identifiers in Python.

Rules for Naming Identifiers

The following rules must be followed when naming identifiers in Python:

  • Case Sensitivity: Identifiers in Python are case-sensitive. This means that Var and var would be treated as two different identifiers.
  • Allowed Characters: Identifiers can only contain letters (uppercase or lowercase), digits (0-9), and underscores (_). They cannot contain special characters like @, !, %, etc.
  • Must Start with a Letter or Underscore: An identifier must start with a letter (a-z, A-Z) or an underscore (_). It cannot start with a digit.
  • No Spaces Allowed: Spaces are not allowed in identifiers. Instead of spaces, you can use underscores (_) to separate words in identifiers, e.g., my_variable.
  • No Reserved Words: Python has a set of reserved words (keywords) that cannot be used as identifiers because they have special meanings in the language. Examples include if, else, for, while, True, and False.

Conventions for Naming Identifiers

In addition to the rules, there are conventions that are commonly followed in Python to make code more readable and maintainable:

  • Lowercase with Underscores: Variable names should typically be written in lowercase letters, with words separated by underscores, e.g., my_variable. This style is often referred to as "snake_case."
  • Uppercase for Constants: Constants (variables that should not change) are often written in all uppercase letters, with words separated by underscores, e.g., MAX_VALUE.
  • Class Names: Class names are typically written in "CamelCase" (also known as "PascalCase"), where each word starts with an uppercase letter and there are no underscores, e.g., MyClassName.
  • Private Variables: To indicate that a variable is intended to be private, you can start its name with an underscore (_), e.g., _private_var. This is a convention and not enforced by Python, but it signals to other programmers that the variable should not be accessed directly.
  • Dunder (Double Underscore) Variables: Names that start and end with double underscores (__) are reserved for special use in the Python language, such as __init__ for class constructors. These are often referred to as "dunder" variables.

Examples of Valid and Invalid Identifiers

Examples of Valid and Invalid Identifiers
Identifier Valid/Invalid Reason
my_var Valid Follows all naming rules and conventions.
Var123 Valid Starts with a letter and contains only letters and digits.
2ndVar Invalid Cannot start with a digit.
my-var Invalid Contains a hyphen, which is not allowed.
my var Invalid Contains a space, which is not allowed.
True Invalid Reserved keyword in Python.
_privateVar Valid Starts with an underscore, indicating it's intended to be private.
__init__ Valid Special method name reserved by Python.

Best Practices for Naming Variables

  • Use Descriptive Names: Choose variable names that clearly describe the data they hold. This makes your code easier to understand. For example, total_cost is more descriptive than tc.
  • Avoid Single-Letter Names: Except for common cases like loop counters (i, j), avoid single-letter names as they often don't provide enough context.
  • Be Consistent: Stick to a consistent naming convention throughout your codebase, such as always using snake_case for variables.
  • Follow PEP 8: Python's official style guide, PEP 8, provides recommendations for naming conventions and other code formatting practices.

Key Takeaway

Naming variables in Python requires adherence to specific rules and conventions to ensure code readability and functionality. By following these guidelines, you can write code that is clean, consistent, and easy to maintain.