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
Never a digit. After the first character you may use letters, digits and underscores in any mix.
A space would split the name in two, and Python would not know what the second half is.
@ # $ % . - and friends are all out. Letters, digits, underscore — that is the whole alphabet of names.
Python has already given those 35 words a job. You cannot take one for a box of yours.
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.
- 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 …)
Python reserves a box, labels it marks and puts 25 in it.
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:
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.
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:
marks = 10
Marks = 20
MARKS = 30
print(marks, Marks, MARKS)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.
total_marks = 123
print(Total_marks)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:
a = 10000
b = 7.5
c = 3
d = (a * b * c) / 100
print(d)2250.0
principal = 10000
rate = 7.5
time_years = 3
simple_interest = (principal * rate * time_years) / 100
print(simple_interest)2250.0
The second one you can still read a month later. The first one you cannot — and neither can the teacher marking 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
marks and Marks are different boxes. And a legal name is not automatically a good one — name the box after what it holds.Which of these is a valid identifier?
Why can't you use for as a variable name?
After marks = 50, what does print(Marks) do?