Some text some message..
Back 2.1 ✅ General Rules for Naming Variables 19 Jun, 2025

In Python, variable naming conventions are essential for code readability and maintainability. Here's a concise breakdown:


General Rules for Naming Variables

  1. Can include: letters (a–z, A–Z), digits (0–9), and underscores (_).

  2. Cannot start with a digit
    ✔️ name1 — Valid
    1name — Invalid

  3. Case-sensitive: Name and name are different.

  4. Cannot use reserved keywords (like for, class, if, def, etc.)


🔤 Naming Conventions (PEP 8 Style Guide)

Style Example Use Case
snake_case user_name, total_amount ✅ Preferred for variable and function names
PascalCase UserProfile, BankAccount ✅ Used for class names
UPPER_CASE MAX_SIZE, DEFAULT_PORT ✅ Used for constants
_single_leading_underscore _internal_var ➖ Used for "private" variables by convention
__double_leading_underscore __hidden_method 🔐 Name mangling in classes (to avoid name clashes)
double_leading_and_trailing __init__, __str__ ⚙️ Reserved for special methods ("magic methods")

Avoid

  • Single letters like x, y, z (unless in short loops or math context)

  • Confusing names like l, O, I (they look like 1, 0)

  • Non-descriptive names: use meaningful and descriptive names


💡 Examples

# Good
total_sales = 1000
is_logged_in = True
user_profile = {'name': 'Abhi'}

# Bad
t = 1000
temp1 = True
n = {'name': 'Abhi'}