Published on

1. execution process in Python

Authors

1. Briefly explain the execution process of a Python file?

🧭 Step-by-Step Execution Process of a Python File:

  1. Read the Source Code (.py file)

    • Python reads your .py file line by line.
  2. Lexing and Parsing

    • The interpreter breaks the code into tokens (lexing) and builds an internal representation (AST – Abstract Syntax Tree).
  3. Compilation to Bytecode

    • Python compiles the parsed code into bytecode — a low-level, platform-independent set of instructions.
    • This bytecode is usually stored in .pyc files inside the __pycache__ directory for reuse.
  4. Interpretation by the Python Virtual Machine (PVM)

    • The PVM executes the bytecode line by line.
    • This is where actual program execution happens — variables are stored, functions are called, etc.

🧠 Visual Summary:

.py file (source code)
Lexer & Parser
Abstract Syntax Tree (AST)
Bytecode (.pyc)
Python Virtual Machine (PVM)
Output / Runtime Behavior

Example:

def greet():
    print("Hello")

greet()
  • Parsed into AST → compiled to bytecode → executed by the PVM → prints "Hello".