So far we have worked only with numeric constants and operators. You can attach a name to a value, and that value will stay around for the rest of your conversational Python session.
Python names must start with a letter or the underbar
_) character; the rest of the name may
consist of letters, underbars, or digits. Names are
case-sensitive: the name
Count is a
different name than
For example, suppose you wanted to answer the question,
“how many days is a million seconds?”
We can start by attaching the name
a value of a million:
>>> sec = 1e6 >>> sec 1000000.0
A statement of this type is called an assignment statement. To compute the number of minutes in a million seconds, we divide by 60. To convert minutes to hours, we divide by 60 again. To convert hours to days, divide by 24, and that is the final answer.
>>> minutes = sec / 60.0 >>> minutes 16666.666666666668 >>> hours=minutes/60 >>> hours 277.77777777777777 >>> days=hours/24. >>> days 11.574074074074074 >>> print days, hours, minutes, sec 11.5740740741 277.777777778 16666.6666667 1000000.0
You can attach more than one name to a value. Use a series of names, separated by equal signs, like this.
>>> total = remaining = 50 >>> print total, remaining 50 50
The general form of an assignment statement looks like this:
name2= ... =
Here are the rules for evaluating an assignment statement:
is some Python variable
name. Variable names must start with either a letter
or the underbar (
_) character, and the
remaining characters must be letters, digits, or
underbar characters. Examples:
is any Python expression.
When the statement is evaluated, first the
evaluated so that it is a single value. For example,
the resulting single value is the integer
Then all the names
are bound to that value.
What does it mean for a name to be bound to a value? When you are using Python in conversational mode, the names and value you define are stored in an area called the global namespace. This area is like a two-column table, with names on the left and values on the right.
Here is an example. Suppose you start with a brand new Python session, and type this line:
>>> i = 5100
Here is what the global namespace looks like after the execution of this assignment statement.
In this diagram, the value appearing on the right shows
int (integer), and the value,
In Python, values have types, but names are not associated with any type. A name can be bound to a value of any type at any time. So, a Python name is like a luggage tag: it identifies a value, and lets you retrieve it later.
Here is another assignment statement, and a diagram showing how the global namespace appears after the statement is executed.
>>> j = foo = i + 1
The expression “
i + 1” is
equivalent to “
5100 + 1”,
i is bound to the integer
5100. This expression reduces to the integer value 5101,
and then the names
foo are both bound to that value. You might think of this
situation as being like one piece of baggage with two
tags tied to it.
Let's examine the global namespace after the execution of this assignment statement:
>>> foo = foo + 1
foo starts out bound to the
integer value 5101, the expression “
1” simplifies to the value 5102.
foo = foo + 1 doesn't make
sense in algebra! However, it is a common way for
programmers to add one to a value.
Note that name
j is still bound to its old