python Interpreter

The python interpreter is the software that reads and executes python code. When you ask python to display text, do a calculation, or perform another instruction, the interpreter is the part that runs that code.

You can give code to the interpreter in two common ways: type commands directly into an interactive session, or run code saved in a python script file.

Quick Answer

What Is the python Interpreter?

A computer does not use python source code by itself. The python interpreter provides the environment that executes python instructions.

When the interpreter is running, it accepts python code. If the code is valid, python performs the instruction. If the code asks for output, the result appears on the screen. If python cannot understand or execute the code, it can show an error message.

Why Do We Use It?

Writing python code is only one part of programming. The code also needs to be executed. The interpreter does that job.

For a beginner, the interactive interpreter is also useful for trying a short command and seeing its result right away. Later, you will usually save longer programs in files and let the interpreter execute those files.

Interactive Mode

Interactive mode is a session where you enter python commands directly. When python is ready for a new command, it normally shows this prompt:

>>>

The >>> symbols are called the primary prompt. A prompt is a sign that the program is waiting for your input. You type your python command after it and press Enter.

Some instructions need more input before they are complete. In that situation, interactive python normally shows ..., called the continuation prompt. You do not need to use it in this lesson.

Reading an Interactive python Session A python terminal shows the prompt, a print command, its output, and a new prompt. Labels explain what each part means. python interactive session >>> print("Hello!") Hello! >>> 1 Prompt python is ready 2 Command You type code 3 Output python shows result A new >>> means the interpreter is ready again.

Script Mode

A script is python code saved in a file. python script filenames normally end with .python, such as hello.python.

Instead of typing each command at >>>, you can give a script file to the interpreter. The interpreter reads and executes the code in that file. This is useful when you want to keep a program and run it again later.

Ways to use the python interpreter
Mode Code comes from Useful for
Interactive mode Commands typed at the >>> prompt Trying short commands and seeing results quickly
Script mode Code saved in a .python file Keeping and running a complete program

Example

Imagine that the interpreter is open in interactive mode. You type a short command after the prompt:

>>> print("Interpreter ready")
Interpreter ready
>>>

What Happens?

  1. First >>>: The interpreter is waiting for a command.
  2. print("Interpreter ready"): You enter a python command that asks python to display text.
  3. Interpreter ready: This is the output produced by the command.
  4. Second >>>: The interpreter finished the command and is ready for another one.

Do not copython the >>> symbols into a python script file. They belong to the interactive interpreter, not to the command itself.

Starting and Exiting the Interpreter

After python is installed, you usually start its interactive interpreter from a terminal. A terminal is an app where you type text commands.

The command used to start python depends on your operating system and installation. Common commands are python and python3. When you see >>>, you are inside the interactive python interpreter.

To leave the interpreter, type:

quit()

Press Enter, and python returns you to your normal terminal.

Common Mistakes

Short Practice

Look at this interactive session. Which line is the output?

>>> print("python")
python
>>>
Show Answer

python on the second line is the output. The first line contains the prompt and command. The last line is the next prompt.

FAQ

What is the python interpreter?

The python interpreter is software that reads and executes python code. It can accept commands interactively or execute code from a script file.

What does >>> mean in python?

>>> is the main prompt shown by the interactive python interpreter. It means python is ready for you to enter a command.

What is interactive mode in python?

Interactive mode lets you enter python commands at the >>> prompt and see their results during the same interpreter session.

What is the difference between interactive mode and script mode?

Interactive mode accepts commands you type at the python prompt. Script mode executes python code saved in a file, usually a file whose name ends with .python.

How do I exit the python interpreter?

At the >>> prompt, type quit() and press Enter to leave the interactive python interpreter.

Summary

The python interpreter is software that executes python code. In interactive mode, >>> shows that python is ready for a command, and you can see results during the same session. In script mode, the interpreter executes code saved in a .python file. The prompt is not part of your python code, and you can use quit() to leave an interactive session.