LambdaLabTM
Computer Science · Class 11 · Variables & User Input
VariablesRules⏱️ 8 min read

Naming Your Variables

The name you write on a box is called an identifier, and you cannot pick just any name. Python has four rules. Break one, and your program does not run at all — not one single line of it.

1The four rules

1
It must start with a letter or an underscore ( _ ).

Never a digit. After the first character you may use letters, digits and underscores in any mix.

marks, _total, total22marks
2
No blank spaces or tab spaces.

A space would split the name in two, and Python would not know what the second half is.

total_markstotal marks
3
No special characters, apart from the underscore.

@ # $ % . - and friends are all out. Letters, digits, underscore — that is the whole alphabet of names.

net_pricenet$price
4
It cannot be a keyword.

Python has already given those 35 words a job. You cannot take one for a box of yours.

class_teacherclass

2Try a name on Python

Type any name into the box below — or press one of the samples. The rules are checked as you type, and if a rule is broken you get the exact error Python would print.

Try a name — Python checks it as you type
=25
  • Starts with a letter or an underscore ( _ ) — never a digit
  • No blank spaces or tabs anywhere in the name
  • Only letters, digits and underscores — no @ # $ % . - and so on
  • Not one of Python's 35 keywords (for, if, class, True …)
✓ Legal name

Python reserves a box, labels it marks and puts 25 in it.

marks25
A broken name stops the whole program
A name that breaks a rule is not a small mistake Python works around. It is a SyntaxError: Python cannot even read the program, so it refuses to run any of it — including the perfectly good lines above the bad one.

3Keywords: the words Python has taken

Keywords (also called reserved words) are words Python has reserved for itself, each with a fixed meaning and purpose. They are the words the language is built from, so you cannot use one as the name of your own box. Python 3 has exactly 35 of them:

FalseNoneTrueandasassertasyncawaitbreakclasscontinuedefdelelifelseexceptfinallyforfromglobalifimportinislambdanonlocalnotorpassraisereturntrywhilewithyield

You do not have to learn this list. You already know a few (True, False, None, not, and, or) and you will meet the rest as you go. Just remember that they are taken.

Your editor tells you
In IDLE, keywords are coloured orange as you type them. If the word you are about to use as a variable name suddenly changes colour, that is Python warning you before the error does.

4marks and Marks are two different boxes

Python is case sensitive. A capital letter is a different letter as far as the language is concerned, so these are three separate variables — three separate boxes in memory:

case_sensitive.py
marks = 10
Marks = 20
MARKS = 30

print(marks, Marks, MARKS)
Output
10 20 30

This mistake gives a NameError more often than any other. You make a box called total_marks, then a few lines later you ask for Total_marks with a capital T, and Python tells you the truth: it has never heard of that box.

one_wrong_letter.py
total_marks = 123
print(Total_marks)
Output
NameError: name 'Total_marks' is not defined. Did you mean: 'total_marks'?

5Legal is not the same as good

a, b and x1 all obey the four rules. Python is perfectly happy with them. But a name has one more job: telling a person what is in the box. These two programs do exactly the same work. Only one of them tells you what it is doing:

hard_to_read.py
a = 10000
b = 7.5
c = 3
d = (a * b * c) / 100
print(d)
Output
2250.0
easy_to_read.py
principal = 10000
rate = 7.5
time_years = 3
simple_interest = (principal * rate * time_years) / 100
print(simple_interest)
Output
2250.0

The second one you can still read a month later. The first one you cannot — and neither can the teacher marking it.

How to name a box well
Name it after what is inside it: total_marks, price, student_name. Write it in small letters, and join words with an underscore — that is the style Python programmers use everywhere. Very short names like i or n are fine for a value you use once and forget, but never for anything important.

6Recap

Key Takeaway
An identifier must start with a letter or an underscore, may then contain letters, digits and underscores, may not contain spaces or special characters, and may not be one of Python's 35 keywords. Names are case sensitive: marks and Marks are different boxes. And a legal name is not automatically a good one — name the box after what it holds.
Quick Check

Which of these is a valid identifier?

Quick Check

Why can't you use for as a variable name?

Quick Check

After marks = 50, what does print(Marks) do?