Python Interpreter
Python is an interpreter-based language. In a Linux system, Python’s executable is installed in /usr/bin/ directory. For Windows, the executable (python.exe) is found in the installation folder (for example C:\python311).
This tutorial will teach you How Python Interpreter Works in interactive and scripted mode. Python code is executed by one statement at a time method. Python interpreter has two components. The translator checks the statement for syntax. If found correct, it generates an intermediate byte code. There is a Python virtual machine which then converts the byte code in native binary and executes it. The following diagram illustrates the mechanism:

Python interpreter has an interactive mode and a scripted mode.
Python Interpreter – Interactive Mode
When launched from a command line terminal without any additional options, a Python prompt >>> appears and the Python interpreter works on the principle of REPL (Read, Evaluate, Print, Loop). Each command entered in front of the Python prompt is read, translated and executed. A typical interactive session is as follows.
>>> price =100>>> qty =5>>> total = price*qty >>> total 500>>>print("Total = ", total) Total =500
To close the interactive session, enter the end-of-line character (ctrl+D for Linux and ctrl+Z for Windows). You may also type quit() in front of the Python prompt and press Enter to return to the OS prompt.
>>> quit() $
The interactive shell available with standard Python distribution is not equipped with features like line editing, history search, auto-completion etc. You can use other advanced interactive interpreter software such as IPython and bpython to have additional functionalities.
Python Interpreter – Scripting Mode
Instead of entering and obtaining the result of one instruction at a time as in the interactive environment, it is possible to save a set of instructions in a text file, make sure that it has .py extension, and use the name as the command line parameter for Python command.
Save the following lines as prog.py, with the use of any text editor such as vim on Linux or Notepad on Windows.
print (“My first program”) price = 100 qty = 5 total = price*qty print (“Total = “, total)
When we execute above program on a Windows machine, it will produce following result:
C:\Users\Acer>python prog.py My first program Total = 500
Note that even though Python executes the entire script in one go, but internally it is still executed in line by line fashion.
In case of any compiler-based language such as Java, the source code is not converted in byte code unless the entire code is error-free. In Python, on the other hand, statements are executed until first occurrence of error is encountered.
Let us introduce an error purposefully in the above code.
print("My first program") price =100 qty =5 total = prive*qty #Error in this statementprint("Total = ", total)
Note the misspelt variable prive instead of price. Try to execute the script again as before −
C:\Users\Acer>python prog.py My first program Traceback (most recent call last): File "C:\Python311\prog.py", line 4, in <module> total = prive*qty ^^^^^ NameError: name 'prive' is not defined. Did you mean: 'price'?
Note that the statements before the erroneous statement are executed and then the error message appears. Thus it is now clear that Python script is executed in interpreted manner.
Python Interpreter – Using Shebang #!
In addition to executing the Python script as above, the script itself can be a selfexecutable in Linux, like a shell script. You have to add a shebang line on top of the script. The shebang indicates which executable is used to interpret Python statements in the script. Very first line of the script starts with #! And followed by the path to Python executable.
Modify the prog.py script as follows −
#! /usr/bin/python3.11print("My first program") price =100 qty =5 total = price*qty print("Total = ", total)
To mark the script as self-executable, use the chmod command
$ chmod +x prog.py
You can now execute the script directly, without using it as a command-line argument.
$ ./hello.py
Interactive Python – IPython
IPython (stands for Interactive Python) is an enhanced and powerful interactive environment for Python with many functionalities compared to the standard Python shell. IPython was originally developed by Fernando Perez in 2001.
IPython has the following important features −
- IPython‘s object introspection ability to check properties of an object during runtime.
- Its syntax highlighting proves to be useful in identifying the language elements such as keywords, variables etc.
- The history of interactions is internally stored and can be reproduced.
- Tab completion of keywords, variables and function names is one of the most important features.
- IPython’s Magic command system is useful for controlling Python environment and performing OS tasks.
- It is the main kernel for Jupyter notebook and other front-end tools of Project Jupyter.
Install IPython with PIP installer utility.
pip3 install ipython
Launch IPython from command-line
C:\Users\Acer>ipython Python 3.11.2(tags/v3.11.2:878ead1, Feb 72023,16:38:35)[MSC v.193464 bit (AMD64)] on win32 Type 'copyright','credits'or'license'for more information IPython 8.4.0-- An enhanced Interactive Python. Type '?'forhelp. In [1]:
Instead of the regular >>> prompt as in standard interpreter, you will notice two major IPython prompts as explained below −
- In[1] appears before any input expression.
- Out[1]appears before the Output appears.
Leave a Reply