Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
BOOK 000 · CHAPTER 1 · Python Interpreter

Python Interpreter

On this pageDefault Python InterpreterExecuting Python Programs

The Python interpreter is a program that reads and executes Python code. It provides an interactive environment for writing and testing Python code.

Default Python Interpreter

To access the Python interpreter by default, open a terminal or command prompt and type python in your activated environment (python3 may be the command on your system). This command will launch the Python interpreter in interactive mode, displaying the familiar >>> prompt within your terminal where you can then type code expressions.

In interactive mode, you can type Python code directly into the interpreter, and it will execute the code immediately. For example:

>>> print("Hello, World")
Output:
Hello, World
Listing 441 — Default Python Interpreter

To exit the Python interpreter, enter exit() or press Ctrl + D on Unix-like systems or Ctrl + Z followed by Enter on Windows.

Executing Python Programs

Executing Python programs is straightforward; you simply call the python command followed by the name of the Python file (with a .py extension) as its first argument. For example, let’s say we have a file named hello_world.py containing the following code:

print("Hello, World")

You can execute it using the following command (assuming hello_world.py resides in your current working directory):

$ python hello_world.py
Hello, World
Listing 442 — Executing Python Programs

While the standard interpreter is fine for quick scripts, more advanced tasks (such as data analysis or scientific computing) often use IPython, an enhanced interactive shell, or Jupyter Notebooks, a web-based environment originally developed as part of the IPython project.