The name we give a variable, also called its identifier, is subject to some important rules. Some of these rules are strict - Python will not accept names that do not follow these rules. These rules are part of the syntax of Python. The syntax of a language is the rules for what are valid ways to write things in the language. Other rules for variable names are human conventions. Python will not enforce these rules, but you and other programmers reading your code may get confused.
Subsection14.2.1Naming Syntax
There are restrictions on what you can use as a variable name.
It must start with a letter (uppercase like A or lowercase like a) or an underscore _
It can also contain digits, like 1 or 9, just not as the first character
It can’t have spaces, or special symbols other than _
It can’t be a Python keyword. Keywords are words that have special meaning in the language(see below for examples).
Case matters. A variable named result is not the same as one named Result.
Python has a few dozen keywords that you can’t use as variable names. Here is a list of the most common ones. If you ever have an error based on one of your variable names and do not know why, compare your name to this list to make sure you are not using a keyword as your name.
Table14.2.1.Python keywords
and
as
assert
break
class
continue
def
del
elif
else
except
exec
finally
for
from
global
if
import
in
is
lambda
nonlocal
not
or
pass
raise
return
try
while
with
yield
True
False
None
Checkpoint14.2.2.
Which of the following is not a legal variable name?
_a1
You can use an underscore as the first character in a variable name
my_name
You can use an underscore between words in a variable name.
amountOfStuff
You can use both uppercase and lowercase letters in a variable name.
BMP
You can use only uppercase letters in a variable name.
1A
You can’t use a digit as the first letter in a variable name.
Checkpoint14.2.3.
Which of the following is not a legal variable name?
_my_name
This is legal, but you don’t usually start a variable name with an underscore.
my name
You can’t have a space in a variable name.
myname
This may be hard to read, but it is legal.
myName
Since you can’t have spaces in names, one way to make variable names easier to read is to use camel case (uppercase the first letter of each new word).
my_name
Since you can’t have spaces in names, one way to make variable names easier to read is to use an underscore between two words.
Subsection14.2.2Naming Convention
Programmers generally choose names for their variables that are meaningful to the human readers of the program — they help the programmer document, or remember, what the variable is used for. To be meaningful, a name must clearly describe what a piece of information is to anyone reading the code, not just the author of the code.
Meaningful names are generally full words like height. Abbreviations are generally not meaningful to anyone other than the original author. If you are reading through code and see a variable h, that will likely not do anything to help you figure out what information it is holding. (The exception to this rule is representing values from a mathematical formula like \(a^b + b^c = c^2\) where a has a well-defined meaning.)
Oftentimes, you need multiple words to meaningfully describe a variable. Say you have a program that involves converting a height between inches and centimeters, within that program, the name height might be confusing. Is it referring to a value in inches? centimeters?
In cases like this, since you can’t have spaces in a variable name, you can either join words together by uppercasing the first letter of each new word like heightInInches (called mixed-case or camel-case because the capitals look like the humps of a camel) or use underscores between words like height_in_inches (called snake-case).
Checkpoint14.2.4.
Say you have a variable that represents the date someone was hired. Which of these would be the most appropriate name?
hireDate
Correct! This or hire_date would both be reasonable choices for a name.
_hire_date
hire_date would be reasonable. But you don’t usually start a variable name with an underscore.
hire date
You can’t have a space in a variable name
date
date isn’t the worst option here, but it might not be clear to other readers of the code what date we are referring to. Is it a person’s hire date? their birthday? today’s date?
hd
An abbreviation would not be appropriate in this case. hd is not meaningful.
Warning14.2.5.
Beginners sometimes confuse “meaningful to the human readers” with “meaningful to the computer”. So they’ll wrongly think that because they’ve called some variable average or pi, it will somehow automagically calculate an average, or automagically associate the variable pi with the value 3.14159.
That is not the case. The computer doesn’t attach semantic meaning to your variable names. The Python interpreter does not care if you call a variable average, a, something, or sue - to it, all of those names are equally meaningful in that they have no real meaning other than naming a value the programmer is working with.