Next / Previous / Contents / TCC Help System / NM Tech homepage

2.2. The assignment statement

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 count.

For example, suppose you wanted to answer the question, “how many days is a million seconds?” We can start by attaching the name sec to a value of a million:

>>> sec = 1e6
>>> sec

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
>>> hours=minutes/60
>>> hours
>>> days=hours/24.
>>> days
>>> 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:

name1 = name2 = ... = expression

Here are the rules for evaluating an assignment statement:

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 its type, int (integer), and the value, 5100.

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”, since variable i is bound to the integer 5100. This expression reduces to the integer value 5101, and then the names j and 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

Because foo starts out bound to the integer value 5101, the expression “foo + 1” simplifies to the value 5102. Obviously, 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 value, 5101.