Python Interpreter


You write your Python code in a text file with a name like hello.py. How does that code Run? There is program installed on your computer named "python3" or "python", and its job is looking at and running your Python code. This type of program is called an "interpreter".

Talk To The Interpreter

A nice benefit of using an interpreter is that you can start interactive version of the interpreter and type Python code right into it to see what it does. What happens if you use + in Python code like this: 'hello' + 3?

You could look up the answer to this, but a very quick way to find out is to type code into the interpreter and see what it does.

There are 2 ways to get the interpreter:

1. Open a command-line terminal. Mac: run the "Terminal" app in the Utilities folder. Windows: type "powershell" in the lower left, this opens the Windows command line terminal. In the terminal type the command "python3" ("python" on Windows). This runs the interpreter program directly. Type ctrl-d to exit when done (on Windows ctrl-z).

2. If you have PyCharm installed, at the lower-left of any window, click the Python Console tab. Or use the Tools > Python Console menu item.

Working With The Interpreter

The interpreter prints a ">>>" prompt and waits for you to type some Python code. It reads what you type, evaluates it, and prints the result - the so called read-eval-print loop (here what the user types is shown in bold)

>>> 1 + 2 * 3
7
>>> 'hello' + 'there'
'hellothere'
>>> max(1, 5, -2)
5
>>> 'hello' + 2
TypeError: can only concatenate str (not "int") to str

This is an excellent way to experiment with Python features to see how they work. If you have some code you are thinking about, it can be quick and informative to fire up the interpreter, and type the code in to see what it does.

Indent In Interpreter

It's possible to write multi-line indented code in the interpreter (we will cover those later in this reader). If a line is not finished, such as ending with ":", hitting return does not run the code right away. You can write further indented lines. When you enter a blank line, the interpreter runs the whole thing. (For throw-away code like this, I am willing to indent just 2 spaces).

>>> for i in range(10):
...   mult = i * 100
...   print(i, mult)
... 
0 0
1 100
2 200
3 300
4 400
5 500
6 600
7 700
8 800
9 900

Interpreter Hacks: dir help

With the special help function, you can pull up the PyDoc for each function (the triple-quote comments for a function). To refer to a function within the str type use the form str.find. Note that the function name is not followed by parenthesis - this is the unusual case of referring to a function but not calling it.

>>> help(str.find)
find(...)
    S.find(sub[, start[, end]]) -> int
    
    Return the lowest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.
    
    Return -1 on failure.