Python Variable Naming Rules

In the last lesson, we learned Python variables. Now let’s learn how to name variables clearly and correctly.

In the last tutorial, we learned what variables are. They are boxes where we store things. And every box has a name.

But here is the thing. We cannot just name our box anything we want.

Imagine we have a pet dog. We want to name him “123” or “my dog!” or “class”. That would be weird, right? And in some places, it might not even be allowed.

Python is the same. It has rules for naming variables. Some rules are hard rules — if we break them, Python gives us an error. Some are soft rules — if we break them, Python does not complain, but other programmers will.

Let’s go through them one by one.

1️⃣ Rule 1: Start with a Letter or Underscore

A variable name must start with a letter (a-z, A-Z) or an underscore (_).

It cannot start with a number.

Good:

name = "Alex"
_age = 25
score1 = 100

Bad:

1name = "Alex" # Error
2age = 25 # Error

Why? Because Python uses numbers for other things. If a name starts with a number, Python gets confused. It does not know if we mean a number or a variable.

So let’s just remember: first character must be a letter or underscore. Not a number.

2️⃣ Rule 2: Only Letters, Numbers, and Underscores

After the first character, we can use letters, numbers, and underscores.

Good:

name = "Alex"
age1 = 25
my_score = 100

Bad:

my-score = 100 # Error (hyphen not allowed)
my score = 100 # Error (space not allowed)
my@score = 100 # Error (@ not allowed)

We cannot use spaces, hyphens, or special symbols like @, #, $, %, etc.

Only letters, numbers, and underscores. That’s it.

3️⃣ Rule 3: No Spaces in Variable Names

Variable names cannot have spaces.

Bad:

my score = 100 # Error

If we want to separate words, we use an underscore.

Good:

my_score = 100

This is why Python programmers love underscores. They make names readable.

4️⃣ Rule 4: Variable Names Are Case Sensitive

Python is case sensitive. This means name, Name, and NAME are three different variables.

name = "Alex"
Name = "Sam"
NAME = "John"
print(name) # Alex
print(Name) # Sam
print(NAME) # John

So let’s be careful. If we create myVariable and later use myvariable, Python will not find it.

This is a common mistake for beginners. Let’s always check our capital letters.

5️⃣ Rule 5: Cannot Be a Python Keyword

We learned about keywords in an earlier tutorial. Keywords are special words that belong to Python. We cannot use them as variable names.

Bad:

if = 5 # Error
else = 10 # Error
class = "Math" # Error

Python will give us a SyntaxError.

Good:

if_condition = True
my_class = "Math"

We can use keywords as part of a longer name. Just not the exact keyword.

6️⃣ Rule 6: Be Descriptive

This is not a hard rule. Python will not give us an error. But it is a very good habit.

Bad:

x = 25
y = "Alex"
z = "London"

Good:

age = 25
name = "Alex"
city = "London"

Which one is easier to understand? The second one, obviously.

When we come back to our code after a month, we will thank ourselves for using good names.

So let’s take two extra seconds and write a meaningful name. It is worth it.

7️⃣ Rule 7: Use snake_case (Python Convention)

Python programmers have a convention. It is not a rule. But almost everyone follows it.

For variable names, we use snake_case.

That means all lowercase letters, with underscores between words.

Good:

first_name = "Alex"
total_score = 100
is_logged_in = True

Bad (but not an error):

firstName = "Alex" # camelCase - used in other languages, not Python style
FirstName = "Alex" # PascalCase - used for classes in Python, not variables

Snake_case is the Python way. Let’s follow it, and our code will look professional.

8️⃣ Rule 8: Avoid Built-in Names

Python has many built-in functions and names. Like print, sum, max, min, list, str, int.

We can technically use these as variable names. Python will not stop us. But we should not.

Why? Because if we do, we overwrite the built-in function. And later when we try to use it, it will not work.

Bad:

print = "Hello"
print("Hi") # Error! print is now a string, not a function

Good:

message = "Hello"
print(message)

So let’s avoid using built-in names. It causes confusion.

9️⃣ Rule 9: Length Matters

Variable names should be long enough to be clear. But not too long.

Too short:

a = 25
b = "Alex"
c = "London"

What is a? What is b? Not clear.

Too long:

the_age_of_the_person = 25
the_name_of_the_person = "Alex"

This is clear, but annoying to type. And it makes the code look cluttered.

Just right:

age = 25
name = "Alex"
city = "London"

Short, clear, and easy to type.

🏠 Python Variable Naming: A Real-Life Example

Think of variable names like names of files on our computer.

We would not name a file asdf.txt or 123.doc. We would name it something like resume.pdf or notes.txt.

Why? Because we want to find it later. We want to know what is inside without opening it.

Variable names are the same. They are labels. Let’s make them clear.

📋 Good vs Bad Variable Names in Python

Let me show us a list.

Bad nameWhy badGood name
xNot descriptiveage
1nameStarts with numbername1
my-nameHas hyphenmy_name
my nameHas spacemy_name
classKeywordstudent_class
printBuilt-inmessage
firstNameNot snake_casefirst_name
a_very_long_variable_name_that_is_hard_to_typeToo longlong_name

Next, we will learn about Numbers in Python so we can continue step by step.