2.2. Rules for Names¶
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 any language are 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.
2.2.1. Naming Syntax¶
There are restrictions on what you can use as a variable name.
It must start with a letter (uppercase like
A
or lowercase likea
) or an underscore_
It can also contain digits, like
1
or9
, just not as the first characterIt 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 namedResult
.
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.
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 |
2.2.2. Naming 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 menaing.)
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).
Caution
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.
Foundations of Python Programming (GNU Free Document License)