A variable is always a reference to a value. Variables do not have types, but objects do. (Python is loosely typed; the same variable may refer to an integer value in the morning and a string value in the afternoon.)

Python does not require variable declarations. However, you cannot access a variable until you have assigned it a value. If you try to access an undefined variable, the interpreter will complain (the wording of the error may be different in your version of Python):

>>> print Scrumptious

Traceback (most recent call last):

File "<stdin>", line 1, in ? NameError: There is no variable named 'Scrumptious'

This example raised an exception. In Python, most errors are represented by exception objects that the surrounding code can handle. Chapter 5 describes Python's exception-handling abilities.

Note Python is case-sensitive. This means that names that are capitalized differently refer to different variables:

>>> FavoriteColor="blue"

>>> favoritecolor="yellow"

>>> print FavoriteColor,favoritecolor blue yellow

Was this article helpful?

0 0

Post a comment