Chapter 1 Exercise Set 0: Chapter Review

Evaluate expression

Using the Python interpreter, type 1 + 2 and then hit return. Python evaluates this expression, displays the result, and then shows another prompt. * is the multiplication operator, and ** is the exponentiation operator. Experiment by entering different expressions and recording what is displayed by the Python interpreter.

Syntax error

Type 1 2 and then hit return. Python tries to evaluate the expression, but it can’t because the expression is not syntactically legal. Instead, it shows the error message:

  File "<interactive input>", line 1
    1 2
SyntaxError: invalid syntax

In many cases, Python indicates where the syntax error occurred, but it is not always right, and it doesn’t give you much information about what is wrong.

So, for the most part, the burden is on you to learn the syntax rules.

In this case, Python is complaining because there is no operator between the numbers.

See if you can find a few more examples of things that will produce error messages when you enter them at the Python prompt. Write down what you enter at the prompt and the last line of the error message that Python reports back to you.

Say “cheese”

Type cheese without the quotation marks. The output will look something like this:

Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
NameError: name 'cheese' is not defined

This is a run-time error; specifically, it is a NameError, and even more specifically, it is an error because the name cheese is not defined. If you don’t know what that means yet, you will soon.

Shell vs. script mode

Type 6 + 4 * 9 at the Python prompt and hit enter. Record what happens.

Now create a python script named with the following contents:

6 + 4 * 9

What happens when you run this script? Now change the contents to:

print(6 + 4 * 9)

and run it again. What happened this time?

Whenever an expression is typed at the Python prompt, it is evaluated and the result is automatically shown on the line below. (Like on your calculator, if you type this expression you’ll get the result 42.)

A script is different, however. Evaluations of expressions are not automatically displayed, so it is necessary to use the print function to make the answer show up.

It is hardly ever necessary to use the print function in shell mode.

Getting to know print

Python scripts are written in text and stored in a text file. You will need a text editor to edit them.


If you are using a GNU/Linux system, especially if it is Debian, instructions for setting up your system are in Appendix A: Configuring Debian for Python Web Development. Now would be a great time to read through Vim and $HOME environment.

If you are on another kind of system, you will need a text editor with which you are comfortable to write your Python scripts.

Write a program named that displays the following then run from the command prompt (with python3

This is line 1.

This is line 2, with a blank line above it.