This guide demonstrates best practices for naming variables and functions in Python, following PEP 8 guidelines and industry standards.
- Names should reveal intent and purpose
- Avoid abbreviations unless universally understood
- Use full words instead of single letters (except in specific contexts)
- Follow the same naming patterns throughout your codebase
- Use established conventions for your project
- Balance between descriptive and overly verbose
- Remove unnecessary words
Python uses snake_case for variable names (lowercase with underscores).
x = 10 # Too vague
n = "John" # Non-descriptive
temp = 25.5 # Ambiguous
data = [1, 2, 3] # Generic
usrAge = 30 # Wrong case (camelCase)
UserName = "Alice" # Wrong case (PascalCase)user_count = 10
student_name = "John"
temperature_celsius = 25.5
student_grades = [1, 2, 3]
user_age = 30
username = "Alice"Use descriptive names that indicate true/false state, often prefixed with is_, has_, can_, should_.
# โ Bad
active = True
logged = False
admin = True
# โ
Good
is_active = True
is_logged_in = False
has_admin_privileges = True
can_edit = False
should_validate = TrueUse plural nouns to indicate multiple items.
# โ Bad
student = ["Alice", "Bob", "Charlie"]
number = [1, 2, 3, 4, 5]
data = {"key1": "value1"}
# โ
Good
students = ["Alice", "Bob", "Charlie"]
sample_numbers = [1, 2, 3, 4, 5]
user_preferences = {"key1": "value1"}
active_connections = set()Use UPPER_CASE_WITH_UNDERSCORES for constants.
# โ Bad
max = 100
pi = 3.14159
apikey = "abc123"
# โ
Good
MAX_CONNECTIONS = 100
PI = 3.14159
API_KEY = "abc123"
DEFAULT_TIMEOUT = 30Python uses snake_case for function names (lowercase with underscores).
def calc(): # Too vague
pass
def getData(): # Wrong case (camelCase)
pass
def process(): # Not specific
pass
def x(): # Meaningless
pass
def doStuff(): # Vague and wrong case
passdef calculate_total_price():
pass
def get_user_data():
pass
def process_payment():
pass
def validate_email_address():
pass
def send_notification():
passFunctions should start with action verbs that describe what they do.
# โ Bad
def user(name):
pass
def email():
pass
# โ
Good
def create_user(name):
pass
def send_email():
pass
def validate_input():
pass
def update_database():
passFor functions that retrieve data.
# โ Bad
def user_by_id(user_id):
pass
def data():
pass
# โ
Good
def get_user_by_id(user_id):
pass
def fetch_customer_data():
pass
def find_matching_records():
passFor functions returning boolean values.
# โ Bad
def valid(email):
pass
def admin(user):
pass
# โ
Good
def is_valid_email(email):
pass
def has_admin_access(user):
pass
def can_delete_file(user, file):
pass
def should_retry(attempt_count):
passUse PascalCase (CapitalizedWords) for class names.
# โ Bad
class user:
pass
class student_record:
pass
class dataProcessor:
pass
# โ
Good
class User:
pass
class StudentRecord:
pass
class DataProcessor:
pass
class EmailValidator:
passSingle letters are acceptable for simple, short loops.
# โ
Acceptable for short, simple loops
for i in range(10):
print(i)
for x, y in coordinates:
print(x, y)Use descriptive names for better readability.
# โ
Better for longer or nested loops
for student_index in range(len(students)):
student = students[student_index]
# ... more complex logic
for row_number in range(grid_height):
for column_number in range(grid_width):
# ... process grid cellUse single leading underscore for internal use.
class UserManager:
def __init__(self):
self._connection = None # Internal variable
def _validate_credentials(self): # Internal method
pass
def login_user(self): # Public method
self._validate_credentials()-
Single Letter Variables (except in specific contexts)
# โ Bad a = 10 b = 20 c = a + b # โ Good width = 10 height = 20 area = width * height
-
Abbreviations
# โ Bad usr_nm = "Alice" calc_avg = lambda x: sum(x) / len(x) # โ Good username = "Alice" calculate_average = lambda x: sum(x) / len(x)
-
Meaningless Names
# โ Bad temp = fetch_data() data = process(temp) result = save(data) # โ Good raw_user_data = fetch_data() validated_user_data = process(raw_user_data) save_status = save(validated_user_data)
-
Generic Names
# โ Bad list1 = [1, 2, 3] list2 = ["a", "b", "c"] # โ Good student_ids = [1, 2, 3] course_codes = ["a", "b", "c"]
| Type | Convention | Example |
|---|---|---|
| Variable | snake_case |
user_count, total_price |
| Function | snake_case |
calculate_total(), get_user() |
| Class | PascalCase |
UserManager, DataProcessor |
| Constant | UPPER_CASE |
MAX_SIZE, API_KEY |
| Private | _leading_underscore |
_internal_method() |
| Boolean | is_/has_/can_ prefix |
is_active, has_permission |
"Programs must be written for people to read, and only incidentally for machines to execute." โ Harold Abelson
Clear, descriptive names make your code:
- โ Easier to understand
- โ Easier to maintain
- โ Easier to debug
- โ More professional
- โ Self-documenting