How Python Works Internally
Python code executes step by step

Introduction :
Python is a high-level, interpreted programming language known for its simplicity and readability. It was created by Guido van Rossum and first released in 1991. Python emphasizes code readability and expressiveness, making it a popular choice for beginners and experienced programmers alike.
On the other hands we can say : " Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. "
So, here a question arise what is interpreted ?
An interpreted language is a type of programming language where instructions are executed directly by an interpreter program, rather than first being compiled into machine code.

How Python Works under the hood In Depth :
Let's have an example :
x = 5
y = 3
z = x + y
print(z)
Here's how Python executes this code:
Lexing (Tokenization) and Parsing : Python first performs lexical analysis (lexing) and parsing on the code. It breaks down the code into tokens (identifiers, operators, keywords) and builds an Abstract Syntax Tree (AST) based on the Python grammar rules.
Bytecode Compilation : Once the code is parsed, Python compiles it into bytecode. Bytecode is a low-level representation of the code that the Python interpreter can understand and execute. Each bytecode instruction corresponds to a specific operation, such as loading a value onto the stack or performing an arithmetic operation.
Execution by Python Virtual Machine (PVM) : The bytecode is executed by the Python Virtual Machine (PVM). The PVM reads each bytecode instruction sequentially and performs the corresponding operation.
In our example :
a) The bytecode instructions for assigning values to
xandyare executed.b) Then, the bytecode instruction for adding
xandyis executed, and the result is stored in a new variablez.c) Finally, the bytecode instruction for printing the value of
zis executed, and the result (8) is printed to the console.Memory Management : Throughout execution, Python manages memory dynamically. Objects like integers (
5,3,8), variables (x,y,z), and bytecode instructions are allocated and deallocated as needed. Python uses techniques like reference counting and garbage collection to manage memory efficiently.Global Interpreter Lock (GIL) : In C-Python (the standard Python implementation), the Global Interpreter Lock (GIL) ensures that only one thread can execute Python bytecode at a time. This prevents multiple native threads from executing Python code simultaneously and simplifies memory management. However, it can also impact the parallelism of multi-threaded Python programs.
In summary, Python executes code by first parsing it into bytecode, which is then executed by the Python Virtual Machine (PVM). Along the way, Python manages memory dynamically and ensures thread safety through mechanisms like the Global Interpreter Lock (GIL).

