Python Variable Naming Rules

In the last lesson you learned about Python Variables. A variable is just a labelled box that holds a value. Now we need to talk about what you are allowed to write on that label. Python has a few hard rules for naming. Break one and your program will not run. Python also has some good habits that keep your code easy to read. Let us go through both.

🤔 Why Naming Rules Matter

Picture this. You write a quick script and call your variables x, xx, and x1. It works today. Then you open it next month and have no idea what any of them hold. Now imagine a name that breaks the rules, like a name that starts with a number. Python will not even run the file. It throws an error and stops.

So naming has two sides:

  • Some rules are hard rules. Break them and Python refuses to run your code.
  • Some rules are conventions. Break them and the code still runs. But it becomes hard for you and other people to read.

The good news is that the rules are short and easy to remember. Let us start with the hard ones.

📏 The Hard Rules

These are the rules Python enforces. If you break one of them, you get a SyntaxError and the program stops.

A variable name in Python:

  • can use letters, digits, and the underscore _
  • cannot start with a digit
  • is case-sensitive, so age and Age are two different names
  • cannot be a Python keyword (a word Python has reserved for itself, like if or for)

Here are some names that follow the rules. The comment after each one says why it is fine.

# ✅ Good: these names are all valid
name = "Alex"
age_25 = 25
total_price = 99.5
_private = "starts with an underscore, that is allowed"
userName = "valid, though Python prefers snake_case"

Now here are names that break the hard rules. Every one of these would stop the program.

# ❌ Avoid: each of these is a SyntaxError
2name = "Riya" # cannot start with a digit
total-price = 99.5 # the dash is read as minus, not allowed in a name
user name = "Arjun" # spaces are not allowed
class = "Physics" # 'class' is a Python keyword

Caution

A space inside a name is not allowed. Python reads user name as two separate things and gets confused. When you want a name with more than one word, join the words with an underscore, like user_name.

🔠 Case Sensitivity Is Real

This one catches a lot of people, so let us be clear about it. Python treats uppercase and lowercase letters as different. So score, Score, and SCORE are three separate variables.

This small program shows it. We make three names that look almost the same and print each one.

score = 10
Score = 20
SCORE = 30
print(score)
print(Score)
print(SCORE)

Output

10 20 30

See how each name held its own value? That is why a typo in the case of a letter can give you a bug. If you store a value in total and later read Total, Python sees a name it has never met and raises an error.

🚫 Keywords You Cannot Use

A keyword is a word Python has already taken for its own grammar. Words like if, else, for, while, class, return, and True mean something special to Python. You cannot use them as variable names. Python would not know whether you mean the keyword or your variable.

If you ever want to check whether a word is a keyword, Python can tell you itself. This short snippet prints the full list.

import keyword
print(keyword.kwlist)

Output

[‘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’]

Tip

You do not need to memorise that list. Most code editors colour keywords differently. If your variable name suddenly turns a different colour, that is a hint that it is a keyword and you should pick another name.

🐍 Use snake_case

Now we move from hard rules to good habits. Python code follows a style where words in a name are lowercase and joined with underscores. This is called snake_case, because the underscores look a bit like a snake lying flat between the words.

You will see a few other styles in other languages. Here is how the same idea looks in each one.

Style Example Used in Python?
snake_case first_name Yes, this is the Python way
camelCase firstName Works, but not the Python style
PascalCase FirstName Saved for class names, not variables

So for everyday variables, use snake_case. Here is what that looks like in real code.

# ✅ Good: snake_case for variable names
first_name = "Alex"
items_in_cart = 3
is_logged_in = True

💬 Pick Clear, Descriptive Names

A name that runs fine can still be a bad name. The best names tell the reader what the value is, without making them guess. Compare these two.

# ❌ Avoid: short names that hide their meaning
tp = 99.5
n = "Riya"
d = 7
# ✅ Good: names that say what they hold
total_price = 99.5
customer_name = "Riya"
days_left = 7

Both versions run exactly the same. But the second one reads almost like plain English. When you come back to this code later, total_price will be much easier to understand than tp.

A short tip on length. You do not need very long names either. the_total_price_of_the_cart is too much. Aim for a name that is clear but not tiring to read, like cart_total.

🔒 Constants in UPPER_CASE

Sometimes you have a value that should never change while the program runs. Think of a tax rate, a maximum login attempt count, or the value of Pi. We call these constants. The habit in Python is to write their names in all capital letters, with underscores between words.

This example sets a couple of constants and uses them.

MAX_LOGIN_ATTEMPTS = 3
TAX_RATE = 0.18
price = 100
final_price = price + (price * TAX_RATE)
print(final_price)

Output

118.0

Note

Python does not actually stop you from changing a value written in UPPER_CASE. The capitals are a message to other people who read the code. They say “this is meant to stay the same, please do not change it.”

⚠️ Common Mistakes

A few naming slips show up again and again. Watch out for these.

  • Starting a name with a digit, like 1st_place. Put the digit later, like place_1, or rename to first_place.
  • Using a dash instead of an underscore. Python reads total-price as total minus price.
  • Forgetting that case matters. Storing into userAge and reading from userage are not the same variable.
  • Using a keyword as a name, like list = [1, 2, 3] or class = "Maths". Pick another word, such as subject = "Maths".
# ❌ Avoid: these will break or confuse
1st_place = "Alex" # cannot start with a digit
final-score = 90 # dash is minus, not part of the name
# ✅ Good: small fixes make them valid and clear
first_place = "Alex"
final_score = 90

✅ Best Practices

Keep these habits and your code stays clean and easy to read.

  • Use snake_case for normal variables and function names.
  • Make names describe the value. email_address beats e.
  • Keep names a sensible length. Clear, but not a whole sentence.
  • Write constants in UPPER_CASE so everyone knows they should not change.
  • Avoid single letters unless it is a tiny loop counter like i.

🧩 What You’ve Learned

✅ The hard rules: names use letters, digits, and underscores, cannot start with a digit, are case-sensitive, and cannot be a keyword.

✅ Python names are case-sensitive, so score and Score are different variables.

✅ Keywords like if, for, and class are reserved and cannot be variable names.

✅ Good habits: use snake_case, pick clear descriptive names, and write constants in UPPER_CASE.

✅ Names that run can still be bad names. Clear names save you time later.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    Which of these is a valid Python variable name?

    Why: user_age uses only letters and an underscore and does not start with a digit, so it is valid.

  2. 2

    How does Python treat the names age and Age?

    Why: Python is case-sensitive, so age and Age are two separate variables.

  3. 3

    Why can't you name a variable class?

    Why: class is a reserved keyword in Python, so it cannot be used as a variable name.

  4. 4

    What naming style does Python prefer for a value that should never change?

    Why: Constants are written in UPPER_CASE with underscores to signal they should not change.

🚀 What’s Next?

Now that your variables have good, legal names, it is time to look closely at the kinds of values they can hold. We will start with numbers.

Numbers in Python

Share & Connect