Python
Naming Convention
Variables
Functions
Coding Standards

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.

python
user_name = "Ada"
item_count = 3
total_price = 19.99

This is preferred over camelCase in normal Python code:

python
userName = "Ada"      # less idiomatic in Python
itemCount = 3

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:

python
1def calculate_total(items):
2    return sum(items)
3
4def send_email(address, subject):
5    print(f"Sending to {address}: {subject}")

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'

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
1MAX_RETRIES = 5
2
3class OrderProcessor:
4    def process_order(self):
5        return "done"
6
7def _internal_helper():
8    return "private-ish"

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:

python
invoice_total = 125.50
retry_delay_seconds = 10

Over:

python
x = 125.50
t = 10

Short names are fine in very small scopes such as simple loops:

python
for i in range(3):
    print(i)

But for domain values, descriptive names usually win.

Avoiding Bad Names

Try not to:

  • shadow built-ins such as list, str, or type
  • use names that differ only by case
  • mix multiple naming styles in one file

For example:

python
list = [1, 2, 3]   # bad idea, shadows built-in list

That code works, but it makes later code harder to reason about.

A Small Example Module

python
1DEFAULT_TIMEOUT_SECONDS = 30
2
3def fetch_user_profile(user_id):
4    return {"id": user_id, "name": "Ada"}
5
6def print_user_profile(user_profile):
7    print(user_profile["name"])
8
9def main():
10    user_profile = fetch_user_profile(42)
11    print_user_profile(user_profile)
12
13if __name__ == "__main__":
14    main()

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 use UPPER_CASE.
  • Prefer descriptive names over short or clever ones.
  • Following PEP 8 makes Python code easier for other developers to read.

Course illustration
Course illustration

All Rights Reserved.