No history yet

Python Naming Conventions

Code Isn't Just for Computers

Writing code that works is only the first step. The real challenge is writing code that humans can understand. When you return to a script months later, or when a colleague needs to modify your work, clarity is everything. This is where naming conventions come in. They are the grammar and punctuation of programming, turning a jumble of instructions into a readable narrative.

In the Python world, the official style guide is PEP 8. It's a set of recommendations designed to make Python code more consistent and readable across different projects and teams. Following these conventions isn't strictly required for your code to run, but it's a critical part of being a professional developer. It signals that you care about teamwork and long-term maintainability.

Think of good naming as leaving a clear trail of breadcrumbs for your future self.

Variables, Functions, and Snake Case

For variables and functions, PEP 8 recommends a convention called snake_case. It's simple: all letters are lowercase, and words are separated by underscores. This style is easy to read and distinguishes these names from other types of identifiers.

A name should do more than just hold a value; it should explain its purpose. This is the core idea of semantic naming. Instead of using generic names like x, data, or temp, choose names that describe the information being stored. A variable named customer_email_address is instantly understandable, whereas str1 is a mystery.

# Bad naming
x = "John Doe"
y = 35

def p(a, b):
    print(f"{a} is {b} years old.")

# Good naming
user_name = "Jane Smith"
user_age = 29

def print_user_summary(name, age):
    print(f"{name} is {age} years old.")

Defining Constants

Sometimes, you'll have a variable whose value is meant to stay the same throughout your program. Think of a tax rate, a file path, or a specific configuration setting. In Python, these are called constants. While the language doesn't technically prevent you from changing their value, the convention is to signal your intent by writing them in all capital letters, with underscores separating words: UPPER_CASE_WITH_UNDERSCORES.

This makes it immediately obvious to anyone reading the code that this value is a fixed part of the program's logic and shouldn't be altered.

# Constants
TAX_RATE = 0.088
MAX_LOGIN_ATTEMPTS = 5

def calculate_final_price(base_price):
    tax_amount = base_price * TAX_RATE
    return base_price + tax_amount

# Calculate price for a 💲100 item
final_price = calculate_final_price(100)
print(f"Final price is: {final_price}")

Names to Avoid

Python has a set of reserved keywords that have special meaning to the interpreter. These words cannot be used as variable, function, or class names because it would create ambiguity and break the language's syntax. Trying to assign a value to a keyword like def or if will result in a SyntaxError.

It's also a bad idea to name a variable after a built-in function, like list or str. While Python allows this, it's a practice known as "shadowing." If you create a variable named list, you can no longer access the original list() function to create lists in that part of your code. This can lead to confusing and hard-to-diagnose bugs.

Common Reserved Keywords to Avoid
and
if
else
for
while
def
class
import
return
True

Ready to test what you've learned about keeping your code clean and readable?

Quiz Questions 1/5

According to PEP 8, what is the recommended naming convention for variables and functions in Python?

Quiz Questions 2/5

Which of the following variable names is the most 'semantic' and best describes its purpose?

Adopting these conventions is a simple way to elevate the quality of your code, making it more professional, readable, and easier to manage as your projects grow.