How Python Works
Underneath
Md Shad Akhtar
IIIT-Delhi
Model of Computer Hardware
● A computer has these key components (simplified view)
● CPU: Computational unit. It has a set of defined instructions like add, mult,
store, read, check_condition, jump_on_condition, …
It can execute these instructions only - data for these instructions is taken from
memory
● Memory: A sequence of memory words; CPU can read/write any individual
memory word by giving its address
● Input/Output Devices: To input or output to/from CPU/Memory from outside -
e.g. keyboard and printer/screen
● Secondary memory (disk): Permanent storage - generally written and read in
blocks to/from memory
CPU Output
Input Main Sec
ALU
Memory Device
Device Cache
Mem
How Computer HW Executes a Program
● A program is a sequence of CPU instructions - has to be loaded in contiguous memory
● Eg. z = x*y will be written as:
● Load R1 x
● Load R2 y
● Mult R1 R2 #R1 ← R1 * R2
● Write R1 z
● The location of first instruction of program is given to CPU
● CPU executes the instruction, loads the next instruction, and keeps repeating this till
"end/stop" (recall there may be jump on condition instructions - for loop and conditionals)
Operating Systems
● Using hardware directly - very hard (have to load the program, give its first location,
manage I/O…)
● First computers built - this had to be done
● Operating Systems (OS): are programs that run on the hardware and provide interface
to users and programmers
● An OS is always running program - which takes input commands from users, gets them
executed on hardware, and provides output
● We almost never see hardware - we only interact with OS - all our interaction with the
computer is through the OS
● The OS provides us a "shell" to give it commands directly, or programs can make
requests: to execute, get memory, open a file,..
How OS executes our Programs
● All programs users write are "applications" - including the programs we may download
(e.g. editor, python compiler,...)
● To execute a program, we have to give the program to OS, which then gets it executed
on hardware
● The program has to be executable – i.e., in the machine language
● When given a user program, OS creates a process to run on CPU, instructs it to execute
the user program code, and assigns it some memory which user program can use (for
variables, etc.)
● The OS mediates the input/output of our program
Applications
Operating System
Main Sec
CPU
Memory Memory
Machine with Operating System
Source: [Link]
Programs in Compiled Language
● We run an editor program (through OS), and create a program using C/C++ syntax, save
it in a file (through OS).
● Run the compiler program, give it our C file as input, and the compiler produces the
machine language code in a file
● Compiler is also a program that can take input and produce output
● This executable file (which is machine language version of our program) we can give to
the OS to run
● The machine language (assembly) has instructions like: LOAD R1 X, STORE R2 Y, ADD R1
R2, MULT R1 R2, ….: each is an operation directly performed by the CPU
Python Programs / Interpreted Languages
● Python works differently - python compilers do not generate machine code for python
programs
● Instead they generate a byte-code - which is "higher level" - in between the low level
machine code and high level Python
● This byte code cannot be run on the machine - instead it is given to an interpreter
● The interpreter is a program which runs on the hardware and which takes the
bytecode and executes them efficiently
● This is why Python (or Java) are called interpreted languages (as compared to compiled
languages) - note that there is a compiler in python also which generates bytecode
which is interpreted
● Bytecode for your program - with some effort you can see it
Python Program Execution - An Overview
Input
Syntax checker Python Virtual
Execution Output
and Translator Machine (PVM)
Run-time error
[Link] Compile-time error [Link] Machine executable code
(byte code) (Internally)
● The python compiler reads the python source code.
○ Checks the syntax
○ Shows error message in syntax; halts translation
○ Some syntax checking is built in the editors / IDEs
● If no syntax error, compiler converts the program to byte code, gives it to Python Virtual Machine
to execute
● PVM accepts the Byte code and converts it into machine executable code. Incase of any error, this
conversion is halted with an error message.
Command-line Execution
● Any python (or any other) program can be executed in following ways.
● GUI-driven
● Online compiler – e.g., hackerrank, colab, etc.
● IDE – Visual Code, PyCharm, etc.
● Command-line (Most fundamental)
● Internally, everything uses command-line calls
Steps for command-line execution
1. Type your program: Open any text editor (sublime, gedit, notepad) and type your program.
2. Save your program: Save the file as [Link]
3. Execute your program:
a. Open terminal or command prompt / window.
b. Locate your saved file or move to the location where your program is located.
c. Use python command to execute your code, i.e., python [Link]
i. If error, go back to your program in the text editor and do necessary modifications and save
it.
ii. No error, your program will run and produce desired results
ByteCode
● Byte Code
● An intermediate code – created using a fixed set of instructions that represent
arithmetic, comparison, memory operations, etc.
● Independent of the OS and hardware – can run on any OS and hardware (a PVM
will be needed)
● It is efficient and faster than original source code statements.
● The .pyc file is not explicitly created as Python handles it internally.
● To display the byte code in an understandable format:
python -m dis [Link]
ByteCode
a, b = 10, 5 Output: 55
c = a * b + b python [Link]
print(c)
● 0 LOAD_CONST 0
((10, 5))
[Link]
2 UNPACK_SEQUENCE 2
4 STORE_NAME 0 (a)
6 STORE_NAME 1 (b)
python -m py_compile [Link]
● 8 LOAD_NAME 0 (a)
python -m dis [Link] 10 LOAD_NAME 1 (b)
12 BINARY_MULTIPLY
14 LOAD_NAME 1 (b)
16 BINARY_ADD
18 STORE_NAME 2 (c)
● 20 LOAD_NAME 3
(print)
22 LOAD_NAME 2 (c)
24 CALL_FUNCTION 1
26 POP_TOP
28 LOAD_CONST 1
Python Runtime System
● Python is a specification of a language
● It has many implementations
● CPython (default - written in C), IronPython, Jython, PyPy, CLPython, Pyston,
Psyco, Cython, IPython, …
● To see which implementation you are using:
import platform
print(platform.python_implementation())
Compiled vs Interpreted
Compiled Interpreted
● Source code translated to machine ● Source translated to intermediate
language code
● Generated code is machine dependent ● Gen code is machine independent
(so compilers for each machine)
● Code execution is slower (as
● Code is generally faster interpreter runs in app mode)
● Compilers can do a lot of compile time ● Interpreters do a lot more checking at
checks runtime
Summary
● Basic machine architecture: CPU, Main memory, Secondary storage, and I/O devices;
CPU executes instructions in machine language, which may load and store data from
main memory
● OS is the software running on the machine which provides an interface between user
programs and hardware
● Compiled languages - programs converted to machine code, then given to OS to
execute
● Interpreted languages - compiled into intermediate language (bytecode), which is then
executed by an interpreter
● Bytecode execution: stack frames which keeps vars of fns/global; eval stack where
bytecode evaluation is done