Warning 2.4.1.
Variable names can never contain spaces.
Bruce
and bruce
are different variables._
) can also appear in a name. It is often used in names with multiple words, such as my_name
or price_of_tea_in_china
. There are some situations in which names beginning with an underscore have special meaning, so a safe rule for beginners is to start all names with a letter.76trombones = "big parade" more$ = 1000000 class = "Computer Science 101"
76trombones
is illegal because it does not begin with a letter. more$
is illegal because it contains an illegal character, the dollar sign. But what’s wrong with class
?class
is one of the Python keywords. Keywords define the language’s syntax rules and structure, and they cannot be used as variable names. Python has thirty-something keywords (and every now and again improvements to Python introduce or eliminate one or two):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 |
A_good_grade_is_A+
+
character is not allowed in variable names.+
character is not allowed in variable names (everything else in this name is fine).DAYS_PER_WEEK = 7 EARTH_GRAVITY = 9.8 POUNDS_PER_KILOGRAM = 2.2046 DISCOUNT_PERCENT = 0.035
POUNDS_PER_KILOGRAM
.