it, you need to import the function into Python. The function’s name is keyword. Listing 4.12
shows you how to import into Python and determine keywords.
LISTING 4.12 Determining Python Keywords
Click here to view code image
>>> import keyword
>>> print (keyword.kwlist)
['False', 'None', 'True', 'and', 'as',
'assert', 'break', 'class', 'continue',
'def', 'del', 'elif', 'else', 'except',
'finally', 'for', 'from', 'global', 'if',
'import', 'in', 'is', 'lambda', 'nonlocal',
'not', 'or', 'pass', 'raise', 'return',
'try', 'while', 'with', 'yield']
>>>
In Listing 4.12, the command import keyword brings the keyword function into the Python
interpreter so it can be used. Then the statement print (keyword.kwlist) uses the keyword
and print functions to display the current list of Python keywords. These keywords cannot be used
as Python variable names.
Creating Python Variable Names
For the first character in your Python variable name, you must not use a number. The first character in
the variable name can be any of the following:
A letter a through z
A letter A through Z
The underscore character (_)
After the first character in a variable name, the other characters can be any of the following:
The numbers 0 through 9
The letters a through z
The letters A through Z
The underscore character (_)
Did You Know: Using Underscore for Spaces
Because you cannot use spaces in a variable’s name, it is a good idea to use
underscores in their place, to make your variable names readable. For example,
instead of creating a variable name like coffeecup, use the variable name
coffee_cup.
After you determine a name for a variable, you still cannot use it. A variable must have a value
assigned to it before it can be used in a Python script.