Chapter 1 Exercise Set 0: Chapter Review

Evaluating expressions

  1. 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. / is the division operator, and // is the floor division operator. A very important operator that is probably new to you is %, the modulus operator.

  2. Experiment by entering different expressions and recording what is displayed by the Python interpreter.

  3. Determine in your head how the Python interepreter will respond to each of the following. You can then check your understanding by “asking” Python.

    1. >>> 4 + 2 * 6
      
    2. >>> 5 % 3
      
    3. >>> 2 ** 3
      
    4. >>> 4 % 5
      
    5. >>> 4 // 5
      
    6. >>> 8 % 5
      

Syntax errors

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

  2. 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”

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

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

    Now create a python script named test1.py 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.

Note

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.

  1. Write a program named using_print1.py that displays the following then run from the command prompt (with python3 using_print1.py):

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