Last modified: May 29, 2026
Python Variables Rules Guide
Variables are the building blocks of any Python program. They store data that your code can use and modify. But Python has strict rules for how you name and use these variables. Breaking these rules causes errors.
This guide covers every rule you must follow. You will learn about naming conventions, allowed characters, reserved keywords, and best practices. Each rule includes simple code examples.
1. Variable Names Must Start with a Letter or Underscore
Python requires variable names to begin with a letter (a-z, A-Z) or an underscore (_). They cannot start with a number.
Valid examples:
name = "Alice" # starts with a letter
_age = 30 # starts with underscore
user1 = "Bob" # starts with letter, number is fine later
Invalid examples:
1st_place = "Gold" # ERROR: starts with a number
@name = "Charlie" # ERROR: starts with a special character
2. Only Letters, Numbers, and Underscores Are Allowed
Variable names can only contain letters, numbers, and underscores. No spaces, hyphens, or special characters like @, !, #, or $.
Correct:my_var, data2, user_name
Incorrect:my-var, user name, data!2
This rule keeps Python code clean and easy to read.
3. Python Keywords Cannot Be Used as Variable Names
Python reserves certain words for its own use. These are called keywords. You cannot use them as variable names. Using a keyword causes a syntax error.
Common keywords include: if, else, for, while, class, def, import, True, False, None.
# This will cause an error
if = 10 # SyntaxError: invalid syntax
SyntaxError: invalid syntax
Always check if a word is a keyword before using it as a variable.
4. Variable Names Are Case-Sensitive
Python treats uppercase and lowercase letters as different. This means name, Name, and NAME are three separate variables.
name = "Alice"
Name = "Bob"
NAME = "Charlie"
print(name) # Output: Alice
print(Name) # Output: Bob
print(NAME) # Output: Charlie
Alice
Bob
Charlie
Important: Using the same name with different cases can confuse readers. Stick to one style.
5. Variable Names Should Be Descriptive
Good variable names explain what the variable holds. Avoid single letters except for counters. Use meaningful words.
Bad example:x = 25 (what is x?)
Good example:user_age = 25
Descriptive names make your code self-documenting. This is a key part of Python's philosophy.
6. Follow the Snake Case Convention
Python uses snake_case for variable names. This means words are separated by underscores and all letters are lowercase. This is not a rule, but it is a strong convention.
# Python convention: snake_case
first_name = "Jane"
last_name = "Doe"
total_score = 95
Other languages use camelCase (like firstName), but in Python, snake_case is preferred.
7. Constants Use Uppercase with Underscores
For values that should not change, use uppercase letters with underscores. This is a convention, not a strict rule, but it signals to other developers that the value is constant.
MAX_SPEED = 120
PI = 3.14159
DEFAULT_COLOR = "blue"
Python does not prevent you from changing these values. The uppercase name is a warning to other programmers.
8. Avoid Using Built-in Function Names
Python has built-in functions like print, input, len, sum, and type. Do not use these as variable names. Overwriting them can break your code.
# BAD: overwriting built-in function
print = "hello" # Now print() no longer works
print("test") # TypeError: 'str' object is not callable
TypeError: 'str' object is not callable
Always check that your variable name does not shadow a built-in function.
9. Variables Must Be Assigned Before Use
You cannot use a variable that has not been assigned a value. This causes a NameError.
# This will cause an error
print(city) # NameError: name 'city' is not defined
NameError: name 'city' is not defined
Always assign a value to a variable before using it in any expression.
10. Variable Names Cannot Be Too Long
While Python has no strict limit, keep names reasonable. Very long names make code hard to read. Aim for 1-3 words. For example, user_phone_number is better than the_phone_number_of_the_current_user.
If you are working with other languages, you might notice differences. For example, JavaScript variables guide shows how JavaScript has similar but slightly different rules. Understanding these differences helps when switching between languages.
11. Use Underscore for Unused Variables
When you need a variable but do not use its value, use a single underscore _. This is a Python convention for throwaway variables.
# Loop where we don't need the index
for _ in range(5):
print("Hello")
This tells other developers that the variable is intentionally unused.
12. Avoid Leading and Trailing Double Underscores
Names like __name__ and __init__ are reserved for Python's special methods. Do not create your own variables with double underscores at the beginning and end. This can cause unexpected behavior.
Exception: Use single leading underscore for "protected" variables (e.g., _internal). Use double leading underscore for name mangling (e.g., __private). But avoid double underscores on both ends.
13. Do Not Use Hyphens or Spaces
Hyphens and spaces are not allowed in variable names. Use underscores instead. Python interprets a hyphen as a minus sign.
Incorrect:user-name (Python sees user - name)
Correct:user_name
14. Variables Can Be Reassigned to Any Type
Python is dynamically typed. This means you can change the type of a variable by assigning a new value of a different type. This is not a naming rule, but it is important to understand.
value = 10 # integer
value = "hello" # now it's a string (perfectly valid)
print(value) # Output: hello
This flexibility is powerful but can lead to bugs if you are not careful.
15. Use Meaningful Prefixes for Clarity
For related variables, use consistent prefixes. This improves readability, especially in larger programs.
# Good prefix usage
user_name = "Alice"
user_age = 30
user_email = "[email protected]"
This pattern helps you group related data mentally.
Conclusion
Following Python's variable naming rules keeps your code clean, readable, and error-free. Start names with a letter or underscore, use only letters, numbers, and underscores, and avoid reserved keywords. Always use descriptive snake_case names and never overwrite built-in functions.
These rules might seem strict at first, but they become second nature with practice. For more examples of how variables work in other languages, check out JavaScript variables examples and types of JavaScript variables. Understanding these fundamentals will make you a better programmer in any language.