The interactive prompt is the easiest way to run your Python programs – simply type the code in this command-line interface. To start the interactive prompt, type python from the Windows command prompt or Linux shell and press Enter:
If the python program can’t be located, you need to enter the full path to the program or add its directory in the PATH variable.
After you start the interactive prompt, the information about the Python version along with some useful information about the operating system will be displayed. Below these information is the >>> prompt, which indicates that you’re in an interactive Python interpreter session. The code you enter after the prompt will be executed once you press Enter. For example, to print the text Hello world, you can use the print function:
C:Python34>python Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In tel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print ('Hello world!') Hello world!
In the example above you can see that the result of the print function was echoed back right away (Hello world!). However, the code you enter in the interactive prompt will not be saved in a file. This is why this prompt is not usually used to very often; it is usually used only for testing or experimental purposes.
You can type multiple Python commands and they will be run immediately after they are entered:
C:Python34>python Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print ('Hello world!') Hello world! >>> x=5 >>> print (x) 5 >>>
To exit the interactive prompt, press Ctrl+Z on Windows or Ctrl+D on Linux.
The Python IDLE (Integrated DeveLopment Environment) editor is a graphical user interface for Python development. This GUI is free and installed automatically during the Python installation. It enables you to edit, run, and debug Python programs in a simple GUI environment.
IDLE is actually a Python program that uses the standard library’s tkinter GUI toolkit to build its windows. It is portable and can be run on all major platforms, such as Windows, Linux, Mac OS, etc. It supports the following features:
- command history and syntax colorization
- auto-indent and unindent for Python code
- word auto-completion
- support for multiple windows
- integrated debugger
The Python IDLE is usually present as an entry in the Start button menu in Windows 7. In Windows 8 and higher versions, you can start it by typing IDLE from the Start menu. Once started, it will display some useful information about the Python version and the operating system:
You can write your code after the >>> prompt and it will be executed when you press Enter:
Although the shell window is useful for executing one-line commands, you will not use it to write full-fledged programs. Instead, Python IDLE comes with its own built-in text editor that you can use to write and save your code. You can start the editor by selecting File > New File:
This opens up a window where you can type your code:
Before running your code, you will need to save it in a file (File > Save). Make sure that the file has the .py extension:
To run your code, click Run > Run Module (or press F5):
The result will be printed in the IDLE shell window:
Notice how the result of our program was displayed after the RESTART line.
REPL – Python Interactive Shell
What is REPL? REPL is the language shell, the Python Interactive Shell. The REPL acronym is short for Read, Eval, Print and Loop.
The Python interactive interpreter can be used to easily check Python commands. To start the Python interpreter, type the command
python without any parameter and hit the “return” key.
The process is:
- Read: take user input.
- Eval: evaluate the input.
- Print: shows the output to the user.
- Loop: repeat.
You can check the version of Python you have installed by typing
python without pressing enter, instead pressing the tab key. This will return the Python versions installed on your computer
|➜ ~ python|
python python2.7 python3.7 python3.7m python3-config python3m python3-pasteurize
python2 python3 python3.7-config python3.7m-config python3-futurize python3m-config python3-wsdump
To get more details on the version, you can type the command:
You should be using Python 3 or newer (2 is legacy).
REPL is the Interactive shell
To start the Python language shell (the interactive shell), first open a terminal or command prompt. Then type the command
python and press enter.
Python then outputs some information like this (including the Python version):
Python 3.7.5 (default, Nov 20 2019, 09:21:52)
[GCC 9.2.1 20191008] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
After the symbols
>>> you can type a line of Python code, or a command.
Each command at the prompt is evaluated by Python. If you would type, if it’s incorrect it returns an error.
|>>> hello world|
File “<stdin>”, line 1
SyntaxError: invalid syntax
In the above example it’s incorrect because text must be between quotes.
|>>> “hello world”|
On some versions of Ubuntu you need type python3 instead of python.
You can type all kinds of commands in the interactive shell. If you want to use it as calculator, just type your calculation:
|>>> 128 / 8|
>>> 256 * 4
Variables can be used in the Python shell too:
|>>> width = 10|
>>> height = 20
>>> size = width*height
Note: We’ll teach you how to start python programs in the next article.
If you started the Python interactive shell, you may be wondering how to exit it. There are several ways to quit the Python interactive shell (REPL).
One way is by typing
exit(). This is calling the function exit, which is why the brackets in
exit() must be written.
A keyboard shotcut can be used to exit the shell too: