What is the naming convention in Python for variables and functions?

12    Asked by LucasTucker in Python , Asked on Sep 26, 2025

What are the standard naming conventions in Python for variables and functions, and how do they help make code readable and maintainable? Understanding these rules ensures your code follows Python’s best practices.

Answered by Nitin Singh

Python has clear conventions for naming variables and functions to improve code readability and maintainability. But what are these naming rules, and how should you apply them in your projects?

1. Variable Naming Conventions

  • Use lowercase letters with words separated by underscores (snake_case).

Example:

 user_name = "Alice"
total_amount = 100

Avoid starting variable names with numbers or using special characters.

2. Function Naming Conventions

  • Functions also follow snake_case.
  • Names should clearly describe the function’s purpose.

Example:

 def calculate_total(price, tax):
    return price + tax

3. Additional Guidelines

Constants are written in UPPERCASE_WITH_UNDERSCORES.

   MAX_USERS = 100

  • Avoid single-character names except for counters or iterators (e.g., i, j).
  • Private methods or variables can start with a single underscore _ to indicate internal use.

4. Benefits of Following Conventions

  • Makes code easier to read and understand for yourself and others.
  • Helps maintain consistency across a project or team.
  • Aligns with Python’s PEP 8 style guide, which is widely adopted in the Python community.

In summary, Python’s naming conventions use snake_case for variables and functions, UPPERCASE for constants, and underscores for private identifiers. Following these conventions improves readability, reduces errors, and ensures your code aligns with standard Python practices.



Your Answer

Interviews

Parent Categories