2  The Python Interpreter

On Linux and macOS you can often find the Python interpreter using the which command. For example:

$ which python
/usr/bin/python

However, this may give you the system version of Python, which depending on your distribution could be anything from an elderly Python 2.7 to the current Python 3 line. We just want to know which interpreter will answer when we call it, because this can affect what features are and aren’t available.

If you’ve installed Python manually, you can also use which to check where it’s installed:

$ which python3
/usr/local/bin/python3
Note

Since the interpreter location is an installation option, the binary may live somewhere else. /usr/local/python is also a common installation location.

Interactive Mode

When commands are read from a tty (TeleTYpewriter), also called a console, shell, or terminal, the Python interpreter is in interactive mode. In this mode Python shows the primary prompt, usually three greater-than signs >>>. For continuation lines, it shows the secondary prompt, usually three dots .... For example:

Python 3.14.6 (default, Jan 22 2019, 21:57:15)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Continuation lines appear when you’re writing multi-line code, such as if statements or for loops. For example:

Python 3.14.6 (default, Jan 22 2019, 21:57:15)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> name = input()
Buffy
>>> if name:
...     print("Hello " + name)
...
Hello Buffy

In the examples throughout this primer, lines beginning with >>> or ... are the code you type. Lines without a prompt are output from Python.

Source Code Encoding

By default, Python source files are treated as UTF-8. Most programmers know UTF-8 as the encoding that lets us write accented characters, symbols, and other text that plain old ASCII can’t handle.

Warning

Python’s standard library sticks to ASCII for identifiers and most source text. That’s still a useful convention for portable code, even though UTF-8 is the default.

If you want to be explicit about UTF-8 support, you’ll need an editor that saves UTF-8, a font that displays the characters you use, and an encoding declaration near the top of the file. It should come before any licence or copyright text, but after the Python hashbang. For example:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

Python supports a heap of encodings you’ll probably never need. You can find the complete list in the Python 3.14 codecs documentation, but there’s absolutely no need to memorise it; the list is mostly here so you know where the map lives if you ever need it.

Checkpoint

Open Python in interactive mode and run a two-line if statement using the >>> and ... prompts. Then create a tiny .py file and run it from the terminal so you can see the difference between interactive input and source-code execution, noting what changes and what stays exactly the same.

Extension Activity

Run python3 -c "import sys; print(sys.executable)" or the Windows equivalent with py -c "import sys; print(sys.executable)". Compare that path with the interpreter your editor uses, and write down which interpreter will run the course examples.

References