🐍Python Internals: What Really Happens When You Run print("Hello, World!") in Python?
A beginner-friendly deep dive into Python internals, bytecode, and execution flow.

When I wrote my first Python program:
print("Hello, World!")
I thought Python simply… ran it. But what actually happens behind the scenes?
Is Python compiled?
Is it interpreted?
How does it understand my code?
Who executes it?
Where is memory stored?
Let’s answer all of these by following the actual execution journey.
🔎 This post gives you the big-picture overview of how Python executes code.
In the upcoming posts, we’ll break down each part — tokenization, parsing, bytecode, memory management, the GIL, and more — in much deeper detail.
🧠 Is Python Compiled or Interpreted?
This is one of the biggest beginner confusions. The short answer:
Python is both compiled and interpreted.
Here’s the simplified execution flow:
Now let’s understand each step.
⚙️ Step 1: Lexical Analysis (Breaking Code into Pieces)
When you write:
x = 5
print(x)
Python doesn’t run it immediately. First, it breaks it into smaller pieces called tokens.
Think of this as Python learning the “words” of your program.
At this stage, it’s just identifying parts — not executing anything yet.
📖 Want to explore this step in depth?
Read Python Internals – Part 2: How Lexical Analysis Works in Python
🌳 Step 2: Parsing (Building a Structure)
After breaking the code into tokens, Python builds a structure called an Abstract Syntax Tree (AST).
Don’t worry — the name sounds scary, but the idea is simple.
It’s just a tree-like structure that represents your code logically.
You can even see it:
import ast
code = "x = 5"
tree = ast.parse(code)
print(ast.dump(tree, indent=4))
Conceptually, it looks like this:
Now Python understands what your code means, not just what it looks like.
🧾 Step 3: Compilation to Bytecode
Here’s something surprising:
Yes — Python compiles your code.
But not into machine code like C or C++.
Instead, it compiles it into something called bytecode.
Bytecode is:
A lower-level version of your Python code
Platform-independent
Not human-readable
Stored in a folder called
__pycache__
Example file:
__pycache__/main.cpython-311.pyc
This bytecode is what actually gets executed. Not your original .py file — but the compiled bytecode version of it.
🖥️ Step 4: Python Virtual Machine (PVM)
Now comes execution.
The Python Virtual Machine (PVM) reads the bytecode and executes it line by line.
You can think of the PVM as:
The engine that actually runs Python programs.
It:
Executes instructions
Manages function calls
Handles memory
Produces output
So technically:
✅ Python is compiled (to bytecode)
✅ Python is interpreted (by the PVM)
Both are true.
🏗️ What is CPython?
Most of us use something called CPython.
CPython is:
The default Python implementation
Written in C
Responsible for compiling and executing Python code
Includes memory management and garbage collection
There are other versions too:
PyPy (faster in some cases)
Jython (runs on JVM)
IronPython (.NET runtime)
But when you download Python from python.org, you’re using CPython.
🧠 How Python Uses Memory
At a high level, Python uses two main memory areas:
📌 Stack
Function calls
Local variables
📌 Heap
Objects
Lists
Dictionaries
Classes
Python manages memory using:
Reference counting
Garbage collection
We’ll explore this properly in the upcoming posts.
🔄 Complete Flow (Simple Summary)
Every time you run:
print("Hello")
Python does this:
🚀 Why Understanding This Matters
When you understand Python internals:
You write better code
You debug more confidently
You understand performance issues
You prepare better for interviews
You think like a deeper-level developer
You move from:
“I use Python”
to
“I understand how Python works.”
And that’s powerful.
📚 Python Internals Series
This is Part 1 of a deeper exploration into how Python works internally.
- ✅ Part 2: Python Internals – Part 2: How Lexical Analysis Works in Python
- 🔜 Part 3: Abstract Syntax Tree (AST)
- 🔜 Part 4: Bytecode Deep Dive
- 🔜 Part 5: Memory Management
- 🔜 Part 6: The Global Interpreter Lock (GIL)
If this helped you, follow along for the full series 🐍

