Python Tutorial8
Python Tutorial8
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:
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.
Learn Python in-depth with real-world projects through our Python certification
course. Enroll and become a certified expert to boost your career.
Save the following lines as prog.py, with the use of any text editor such as vim on
Linux or Notepad on Windows.
Open Compiler
print ("My first program")
price = 100
qty = 5
total = price*qty
print ("Total = ", total)
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.
Open Compiler
print ("My first program")
price = 100
qty = 5
total = prive*qty #Error in this statement
print ("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.
Open Compiler
#! /usr/bin/python3.11
$ chmod +x prog.py
You can now execute the script directly, without using it as a command-line argument.
$ ./hello.py
C:\Users\Acer>ipython
Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934
64 bit (AMD64)] on win32
Type 'copyright', 'credits' or 'license' for more information
IPython 8.4.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
Instead of the regular >>> prompt as in standard interpreter, you will notice two major
IPython prompts as explained below −
IPython's magic functions are extremely powerful. Line magics let you run DOS
commands inside IPython. Let us run the dir command from within IPython console
Directory of F:\Python311