What is the naming convention in Python for variables and functions?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Python naming conventions are mostly defined by PEP 8, the language’s style guide. For variables and functions, the standard convention is lowercase_with_underscores, often called snake_case, because it is easy to read and consistent with the wider Python ecosystem.
The Standard Style for Variables
Variable names in Python are usually written in lowercase words separated by underscores.
This is preferred over camelCase in normal Python code:
The goal is not just aesthetics. Consistent naming makes code easier to scan, especially in larger modules where variables, functions, and classes all need to look distinct.
The Standard Style for Functions
Function names follow the same pattern:
Function names are typically verbs or verb phrases because they describe actions.
Good examples:
- '
load_config' - '
send_email' - '
parse_response'
Less clear examples:
- '
data' - '
thing' - '
handleStuff'
Related Naming Conventions You Should Know
Even though the question is about variables and functions, these related conventions help the whole style system make sense:
- classes use
CapWords - constants use
UPPER_CASE_WITH_UNDERSCORES - internal names often start with a single underscore
Example:
Python does not enforce these rules, but most teams follow them because they make code easier to read.
Why Snake Case Is Preferred
Python’s standard library, most popular frameworks, and most community examples use snake case for variables and functions. Matching that style means your code feels normal to other Python developers.
That matters when:
- onboarding teammates
- reading framework documentation
- publishing libraries
- returning to your own code months later
A style guide is valuable because it reduces unnecessary variation.
Descriptive Names Matter More Than Clever Names
The convention gives you a format, but the actual word choice still matters.
Prefer:
Over:
Short names are fine in very small scopes such as simple loops:
But for domain values, descriptive names usually win.
Avoiding Bad Names
Try not to:
- shadow built-ins such as
list,str, ortype - use names that differ only by case
- mix multiple naming styles in one file
For example:
That code works, but it makes later code harder to reason about.
A Small Example Module
This shows the typical visual rhythm of Python naming conventions: constants in uppercase, functions in snake case, and variables also in snake case.
Common Pitfalls
One common mistake is importing habits from JavaScript, Java, or C# and writing function names in camelCase. Python developers can read it, but it looks out of place.
Another issue is choosing names that are technically valid but not descriptive. A style guide cannot rescue unclear naming choices.
A third pitfall is shadowing Python built-ins with variable names. That creates subtle bugs and confusing tracebacks later.
Summary
- Python variables and functions normally use
snake_case. - Function names are usually verbs or action phrases.
- Classes use
CapWords, while constants useUPPER_CASE. - Prefer descriptive names over short or clever ones.
- Following PEP 8 makes Python code easier for other developers to read.

