Table of Contents
TogglePython is an interpreter-based language. In a Linux system, Python’s executable is installed in the /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 the Python interpreter works in interactive and scripted modes. Python code is executed one statement at a time. The Python interpreter has two components. The translator checks the statement for syntax. If it’s correct, it generates intermediate bytecode. The Python Virtual Machine (PVM) then converts this bytecode into native binary and executes it. Below is a diagram illustrating the process.
When you launch Python from a command line terminal without any additional options, the Python prompt (>>>) appears. The interpreter works based on the REPL (Read, Evaluate, Print, Loop) principle. Each command entered is read, translated, and executed.
A typical interactive session looks like this:
>>> price = 100
>>> qty = 5
>>> total = price*qty
>>> total
500
>>> print("Total = ", total)
Total = 500
To exit the interactive session, press Ctrl+D (Linux) or Ctrl+Z (Windows), or type quit() and press Enter.
In scripting mode, you can save Python commands in a text file with a .py extension. Then, use the file name as a command-line argument to the Python interpreter.
Example of a simple script (prog.py):
print("My first program")
price = 100
qty = 5
total = price * qty
print("Total = ", total)
When you execute this on a Windows machine, the output will be:
C:\Users\Acer>python prog.py
My first program
Total = 500
If an error occurs in the script, Python will stop execution and show the error message. For example, the following error:
print("My first program")
price = 100
qty = 5
total = prive * qty # Error: misspelt variable
print("Total = ", total)
When you run the script, it will produce the error:
C:\Users\Acer>python prog.py My first program Traceback (most recent call last): File "C:\Python311\prog.py", line 4, intotal = prive*qty ^^^^^ NameError: name 'prive' is not defined. Did you mean: 'price'?
Python scripts in Linux can also be self-executable, like a shell script, by adding a shebang line at the top of the file. The shebang points to the Python interpreter, indicating how to execute the script.
Example of using the shebang:
#! /usr/bin/python3.11
print("My first program")
price = 100
qty = 5
total = price * qty
print("Total = ", total)
Make the script executable with the following command:
$ chmod +x prog.py
You can now run the script directly:
$ ./prog.py
IPython is an enhanced interactive environment that provides many useful features compared to the standard Python shell. It was originally developed by Fernando Perez in 2001 and is the main kernel for Jupyter notebooks.
IPython provides the following features:
To install IPython, use pip:
pip3 install ipython
After installation, you can launch IPython with the command:
C:\Users\Acer>ipython
IPython will display a prompt like this:
In [1]: price = 100
In [2]: quantity = 5
In [3]: total = price * quantity
In [4]: total
Out[4]: 500
