Inner Working of PYTHON

Rishab KumarRishab Kumar
2 min read

INTRODUCTION TO PYTHON

Python is an interpreted, high-level programming language that uses a multi-step execution process that convert source code into executable instructions.

PYTHON EXECUTION FLOW

Source Code (Python file ‘.py’) —> Lexical Analysis (Tokenization) —> Parsing (Concrete Syntax Tree ‘CST ‘) —> AST Generation (Abstract Syntax Tree) —> Bytecode Compilation (Platform-independent intermediate code) —> Bytecode Caching (.pyc files) —> Python Virtual Machine (‘PVM ‘/ Runtime Engine) —> Program Output (Final Execution Result)

Source Code (Python file ‘.py’) - Human-readable Python code with comments, whitespace, and syntax.

Lexical Analysis (Tokenization) - Breaks code into tokens: keywords, identifiers, operators, literals.

Parsing (Concrete Syntax Tree ‘CST ‘) - Creates grammatical structure tree including all syntax elements.

AST Generation (Abstract Syntax Tree) - Removes comments, whitespace, redundant syntax. Keeps only logical structure.

Bytecode Compilation (Platform-independent intermediate code) - Low-level instructions for Python Virtual Machine.

Bytecode Caching (.pyc files) - Cached in __pycache__ directory for imported modules
Format: filename.cpython-.pyc.

Python Virtual Machine (‘PVM ‘/ Runtime Engine) - Executes bytecode instructions with dynamic typing and memory management.

Program Output (Final Execution Result) - Console output, file operations, GUI displays, etc.

CHARACTERISTICS

Dynamic Typing: Types determined at runtime, not compile-time.

Platform Independent: Bytecode runs on any system with Python.

Interpreted: Bytecode is interpreted, not compiled to machine code.

Stack-based: PVM uses stack operations for execution.

IMPLEMENTATIONS

CPython (Standard)

Jython (Java-based)

IronPython (C#/.NET)

PyPy (JIT compiler)

Stackless (Microthreads)

10
Subscribe to my newsletter

Read articles from Rishab Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rishab Kumar
Rishab Kumar