Chapter 1: The Basics / Lesson 3

Constants and Naming Conventions

5 min ยท The Basics

Constants in Python

Unlike some programming languages, Python doesn't have a built-in constant type that prevents values from being changed. However, Python programmers use a naming convention to indicate that certain variables should be treated as constants - values that shouldn't change during program execution.

By convention, we use SCREAMING_SNAKE_CASE (all uppercase letters with underscores) to indicate a variable is a constant. While Python won't prevent you from changing these values, other programmers will understand they shouldn't be modified.

constants.py
# Mathematical constants PI = 3.14159 E = 2.71828 GRAVITY = 9.81 # Application constants MAX_SPEED = 120 MIN_AGE = 18 APP_NAME = "My Python App" # Configuration constants DEFAULT_PORT = 8080 TIMEOUT_SECONDS = 30 MAX_RETRIES = 3

When to Use Constants

Constants are perfect for values that are used multiple times throughout your program and shouldn't change. They make your code more maintainable:

using_constants.py
TAX_RATE = 0.08 # 8% tax # Use the constant throughout your code price1 = 100 total1 = price1 + (price1 * TAX_RATE) price2 = 50 total2 = price2 + (price2 * TAX_RATE) # If tax rate changes, you only update it in one place! # Without constants, you'd have to find and update every occurrence

Python Naming Conventions

Python follows specific naming conventions that make code more readable and help other programmers understand your code. These conventions are part of PEP 8, Python's official style guide:

  • snake_case โ€” for variables and functions: my_variable, calculate_total, user_name
  • SCREAMING_SNAKE_CASE โ€” for constants: MAX_VALUE, PI, API_KEY
  • PascalCase โ€” for classes: MyClass, UserAccount, DatabaseConnection
  • _private โ€” leading underscore for internal use: _internal_var, _helper_function
  • __special__ โ€” double underscores for special methods (rarely used): __init__

Naming Convention Examples

Here are practical examples of each naming convention:

naming_examples.py
# Variables and functions (snake_case) user_name = "Alice" total_price = 99.99 def calculate_total(): pass # Constants (SCREAMING_SNAKE_CASE) MAX_CONNECTIONS = 100 DEFAULT_TIMEOUT = 30 API_BASE_URL = "https://api.example.com" # Classes (PascalCase) class UserAccount: pass class DatabaseConnection: pass # Private variables (leading underscore) _internal_counter = 0 def _helper_function(): pass

Reserved Keywords

Python has reserved keywords that cannot be used as variable names. These words have special meaning in Python:

โš ๏ธ Python Keywords (Cannot Use as Variables)

False, None, True, and, as, assert, async, await, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield

keywords.py
# These will cause errors: # if = 10 # 'if' is a keyword # class = "A" # 'class' is a keyword # def = 5 # 'def' is a keyword # Instead, use descriptive names: condition = 10 class_name = "A" definition = 5

Best Practices for Naming

โœ… Good Naming Practices

โ€ข Use descriptive names: user_age instead of x

โ€ข Be consistent: use the same naming style throughout

โ€ข Use meaningful abbreviations: num for number, temp for temperature

โ€ข Avoid single letters except in loops: i, j, k for iterators

โŒ Naming Mistakes to Avoid

โ€ข Too short: n, d, x - unclear what they represent

โ€ข Too generic: data, value, temp - not specific enough

โ€ข Using reserved keywords: if, class, def - will cause errors

โ€ข Inconsistent casing: mixing camelCase and snake_case

Common Naming Patterns

Here are some common patterns you'll see in Python code:

patterns.py
# Boolean variables often start with 'is', 'has', 'can' is_active = True has_permission = False can_edit = True # Counters and totals count = 0 total_price = 0.0 item_count = 10 # Temporary variables temp_value = 5 current_user = "Alice" # Loop variables (can be short) for i in range(5): print(i) for item in items: print(item)

Why Naming Matters

Good naming makes your code self-documenting. Compare these examples:

bad_vs_good.py
# Bad naming - unclear what this does x = 25 y = 8 z = x + y print(z) # Good naming - self-explanatory base_price = 25 tax_amount = 8 total_price = base_price + tax_amount print(total_price)

The second version clearly shows you're calculating a total price with tax, while the first version is cryptic.

๐ŸŽ‰

Lesson Complete!

Great work! Continue to the next lesson.

main.py
๐Ÿ“ค Output
Click "Run" to execute...