“91974˙CH01˙ﬁnal” — 2012/12/14 — 13:55 — page9—#9
1.5 Python Overview 9
Figure 1.2 The sum of the numbers 1 to N is
1.5 Python Overview
In this book the language you will use to write your computer programs is called Python.
Why did we choose Python instead of a language like C++ or Java? The answer is simple:
We want you to focus on learning the problem-solving strategies and techniques that a com-
puter scientist uses. Programming languages are tools and Python is a good beginning
tool. Languages like Java and C++ are ﬁne tools as well, but they require you to keep
track of many more details and they are harder to learn than Python.
The best way to learn Python is to try it out—so let’s get started. The ﬁrst thing we
are going to do is start the Python interpreter. Depending on your operating system,
there are any number of ways to do this. For example, you might start a program called
IDLE—named after Eric Idle of Monty Python fame. Or you might just type Python at
the command prompt. No matter how you start it, you will know you are successful when
you see a window such as the one shown in Figure 1.3. In this case, we have started the
Python interpreter from a terminal window on a MacBook Pro. For detailed instructions
on installing and starting Python on your system, refer to Appendix A.
As you progress through this chapter, you will see that example programs are in boxes
called listings, and commands that you can type interactively at the Python shell are in
boxes called sessions. Whenever you see a session box, we strongly encourage you to try
the session for yourself. Also, once you have typed in the example we have shown, feel free
to try some variations in order to ﬁnd out for yourself what works and what does not.
“91974˙CH01˙ﬁnal” — 2012/12/14 — 13:55 — page 10 — #10
10 CHAPTER 1 Introduction
Figure 1.3 The Python shell
As we begin to explore Python, we will answer three important questions you should ask
about any programming language:
What are the primitive elements?
How can we combine the primitive elements?
How can we create our own abstractions?
1.5.1 Primitive Elements
At the deepest level, the one primitive element in Python is the object. In fact, everything
in Python is an object, and you will read this refrain often in this book. By now you are
probably wondering what we mean by object. After all, if you look around you will see
many objects: this book, pencils, pens, your chair, a computer. What do these items have
to do with Python? Like you, Python thinks of the things in its world as objects. Python
even considers numbers to be objects—an idea that may be a bit confusing to you as you
probably don’t think of numbers as objects. But Python does, and we’ll see why this is
“91974˙CH01˙ﬁnal” — 2012/12/14 — 13:55 — page 11 — #11
1.5 Python Overview 11
Python classiﬁes the diﬀerent kinds of objects in its world into types. Some of the
easiest types to work with are numbers. Python knows about several diﬀerent types of
Floating point numbers
Integers are the whole numbers that you learned about in math class. We will introduce
more of Python’s primitive types as we progress through this chapter. But before we move
on let’s look at Python’s integers in more detail. We can already do a lot with Python just
using integers. For starters, we can use the Python shell we started a few moments ago
as a calculator. Let’s try a few mathematical expressions. Type in the following examples
using the Python interpreter. After you have typed in an expression, press the return key
to see the result.
>>> 15 % 2
Session 1.1 Simple integer math
The examples in Session 1.1 illustrate some very important Python concepts that you should
become familiar with as soon as possible. The ﬁrst concept is Python’s evaluation loop.