In Python, variable naming conventions are essential for code readability and maintainability. Here's a concise breakdown:
Can include: letters (a–z, A–Z), digits (0–9), and underscores (_).
Cannot start with a digit
✔️ name1
— Valid
❌ 1name
— Invalid
Case-sensitive: Name
and name
are different.
Cannot use reserved keywords (like for
, class
, if
, def
, etc.)
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") |
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
# Good
total_sales = 1000
is_logged_in = True
user_profile = {'name': 'Abhi'}
# Bad
t = 1000
temp1 = True
n = {'name': 'Abhi'}