Module 2 · Python Syntax and Fundamentals — topic 9 of 14
Naming conventions
Beyond what is allowed, Python has agreed styles for names: snake_case for variables and functions, PascalCase for classes and UPPER_CASE for constants. Learn them and why they matter.
Learning objectives
- Apply snake_case, PascalCase and UPPER_CASE correctly
- Choose descriptive, honest names
- Recognise the meaning of leading underscores
Definition
Beyond what is allowed, Python has agreed styles for names: snake_case for variables and functions, PascalCase for classes and UPPER_CASE for constants. Learn them and why they matter.
Introduction
The previous lesson covered which names Python accepts. Naming conventions are the community’s agreement about which names to choose, so that code written by different people looks consistent. They come from PEP 8, Python’s style guide.
Why it matters
Conventions carry information. Seeing MAX_RETRIES tells you it is a constant; seeing StudentRecord tells you it is a class; seeing calculate_total tells you it is a function or variable. Following them makes your code instantly familiar to other Python programmers.
Detailed explanation
Variables and functions: snake_case, all lower-case with underscores, such as monthly_salary and send_reminder().
Classes: PascalCase (also called CapWords), such as BankAccount and LibraryMember.
Constants (values that should not change): UPPER_CASE, such as GST_RATE = 0.18.
Modules (file names): short lower-case names, such as grade_calculator.py.
A single leading underscore (_cache) means “internal, not for outside use”. Double underscores on both sides (__init__) are reserved for Python’s special names.
Good names are descriptive and honest: is_valid for a Boolean, students for a list of students, count_students() for a function that counts.
Plural names suit collections and singular names suit single items, which makes loops read naturally: for student in students. Functions are usually named with a verb because they do something (calculate_fee, load_marks), while variables are named with nouns because they hold something (fee, marks).
Consistency within a project matters as much as the conventions themselves. If one file calls something customer_id, every other file should use the same name rather than cust_id or client_number.
Syntax
- MAX_SEATS: constant in UPPER_CASE.
- BatchRegister: class in PascalCase.
- seats_remaining and booked_seats: function and variable in snake_case.
Code examples
Constants and snake_case
590.0
Boolean names read like questions
True
More examples
Renaming for clarity
1700.0
Step-by-step explanation
- The comment shows an unclear original with single-letter names.
- DISCOUNT_PERCENT is a constant, so it is upper-case.
- discounted_price says what the function returns; its parameters say what they hold.
- The behaviour is identical, but the new version needs no explanation.
Expected output
Running “Renaming for clarity” prints exactly:
Practical applications
- Linters such as Ruff and pylint enforce PEP 8 naming automatically.
- Large codebases are searchable because names follow predictable patterns.
- Interviewers notice clear, conventional naming in coding exercises.
Best practices
- snake_case for variables and functions, PascalCase for classes, UPPER_CASE for constants.
- Name Booleans like questions: is_, has_, can_.
- Avoid single letters except for short loop counters such as i.
Common mistakes
camelCase for variables
Avoid
Prefer
camelCase is common in JavaScript and Java, but Python convention is snake_case.
Vague names
Avoid
Prefer
Specific names make the purpose of each value obvious.
Key takeaways
- PEP 8 defines Python’s naming conventions.
- snake_case, PascalCase and UPPER_CASE each signal a kind of name.
- Descriptive names make code self-explanatory.
Try it yourself
- 1Rename these to follow conventions: TotalPrice (variable), bank_account (class), pi (constant).
- 2Rewrite def calc(x): return x * 0.2 with descriptive names.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
10 questions cover the “Python Syntax and Fundamentals” module. Answers and explanations are revealed after you submit.
- BeginnerWhat happens when this code runs?
- BeginnerWhich line is a comment in Python?
- BeginnerWhich of these is a Python keyword?