Chapter 1 Exercise Set 1 Solutions

Evaluate Expression

Here are the python shell’s results from this exercise:

>>> print(1 + 2)
3
>>> print(3 * 4)
12
>>> print(3 ** 2)
9

Syntax Error

>>> 3d = 2
  File "<stdin>", line 1
    3d = 2
     ^
SyntaxError: invalid syntax
>>>

This is one example of another syntax error. Student answers will vary.

Shell vs. Script Mode

At the prompt, you will see:

>>> 6 + 4 * 9
42

When you run test1.py with just that expression, you will not see any output. When you add the expression to a print function you will again see 42 as output.

The python shell prints the value of any expression typed at the prompt. Python scripts do not behave this way, which is a good thing, or they would constantly be spewing output the user of the program should not see.

Remember the suggestion in chapter to think of the shell as “scratch paper”, and it may be clearer to you why the shell behaves the way it does.

Getting to know print

Click using_print1.py to download a copy of this program.

print("This is line 1.")
print()
print("This is line 2, with a blank line above it.")