“46029˙CH01˙Miller” 2008/8/11 7:03 page 17 #17
1.5 Python Overview 17
int or float functions. For example, float(5) will convert the integer 5 to the floating-
point number 5.0. When converting floating-point numbers to integers, Python always
truncates the fractional part of the number. For example, int(3.99999) will convert the
floating-point number 3.99999 to the integer 3.
In summary, we have seen that Python supports several different types of primitive objects
in the number family: integers for ordinary simple math; or, when precision is required or
when dealing with very large numbers; floating-point numbers, for working with scientific
applications or accounting applications where we need to keep track of dollars and cents.
We have seen that Python can be used to make simple numerical calculations. However, at
this point Python is nothing more than a calculator. In the next section we will add some
additional Python primitives that will give us a lot more power.
1.5.2 Naming Objects
Very often we have an object that we would like to remember. Python allows us to name
objects so that we can refer to them later. For example, we might want to use the name pi
rather than the value 3.14159 in a mathematical expression. We might also want to give a
name to a value that we are going to use over and over again rather than recalculating it
each time.
In Python we can name objects using an assignment statement. A statement is like an
expression except that it does not produce a value for the read–eval–print loop to print.
An assignment statement has three parts: (1) the left-hand side, (2) the right-hand side,
and (3) the assignment operator (=). The left side contains the name we are assigning to
a variable, and the right side can be any Python expression.
variableName = python expression
When the Python interpreter evaluates an assignment statement, it first evaluates the
expression that it finds on the right-hand side of the equals sign. Once the right-hand side
expression has been evaluated, the resulting object is referred to using the name found on
the left side of the equals sign. In computer science, we call these names variables. More
formally, we define a variable to be a named reference to a data object. In other words, a
variable is simply a name that allows us to locate a Python object.
Suppose we want to calculate the volume of a cylinder where the radius of the base is 8 cm
and the height is 16 cm. We will use the formula volume = area of base * height. Rather than
calculate everything in one big expression, we will divide the work into several assignment
statements. First, we will name the numeric objects “pi,” “radius,” and “height.” Next,
“46029˙CH01˙Miller” 2010/2/12 11:33 page 18 #18
18 CHAPTER 1 Introduction
we will use the named objects to calculate the area of the base and finally the volume of
the cylinder. Session 1.5 shows how we use this sequence of assignment statements and
Python arithmetic to solve our problem.
>>> pi = 3.14159
>>> radius = 8.0
>>> height = 16
>>> baseArea = pi * radius ** 2
>>> cylinderVolume = baseArea * height
>>> baseArea
>>> cylinderVolume
Session 1.5 Calculating the volume of a cylinder with assignment statements
After studying Session 1.5, you may have some questions:
How is the use of the equals sign in Python different from what you learned in math
If you change the value for baseArea will cylinderVolume automatically change?
Why doesn’t Python print out the value of pi after the first assignment statement?
What are the legal values for names in Python?
Let’s look at these questions one at a time. The equals sign in Python is very different from
what you learned in math class. In fact, you should think of it not in terms of equality
but rather as the assignment operator, which has the job of associating a name with an
object. Figure 1.5 illustrates how names are associated with objects in Python. All the
names and objects in this figure come from Session 1.5. The relationships between names
and the objects they reference are indicated by the arrows between them.
Another way of thinking about assignment is to imagine that an assignment statement is
like taking a sticky label with a name written on it and attaching it to an object. You
know that you can put more than one sticky label on an object in the real world, and the

Get Python Programming in Context now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.