Page 53 - Python Basics: A Practical Introduction to Python 3
P. 53
3.3. Create a Variable
>>> greeting = "Hello, World"
>>> print(Greeting)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Greeting' is not defined
If you have trouble with an example in this book, double-check that
every character in your code—including spaces—matches the example
exactly. Computers have no common sense, so being almost correct
isn’t good enough!
Rules for Valid Variable Names
Variable names can be as long or as short as you like, but there are a
few rules that you must follow. Variable names may contain upper-
case and lowercase letters (A–Z, a–z), digits (0–9), and underscores
(_), but they cannot begin with a digit.
For example, each of the following is a valid Python variable name:
• string1
• _a1p4a
• list_of_names
The following aren’t valid variable names because they start with a
digit:
• 9lives
• 99_balloons
• 2beOrNot2Be
In addition to English letters and digits, Python variable names may
contain many different valid Unicode characters.
Unicode is a standard for digitally representing characters used in
most of the world’s writing systems. That means variable names can
contain letters from non-English alphabets, such as decorated letters
52