Python 3 Tutorial: Architecture, Execution Model & First Script

A comprehensive introduction to Python 3, bytecode compilation, CPython interpreter, and hands-on programming.

Python 3 Tutorial: Architecture, Execution Model & First Script
What You Will Learn

In this guide, we demystify how Python transforms source code into bytecode, how the CPython virtual machine operates, and how to write clean, idiomatic Python scripts with modern tooling.

1. The Python Execution Pipeline

Unlike purely compiled languages (like C++ or Rust) that produce native machine instructions directly, Python is an interpreted, bytecode-compiled language. The compilation occurs transparently behind the scenes:

1. Source Code

script.py contains human-readable UTF-8 Python code.

2. Bytecode Compiler

Compiled to intermediate instructions saved in .pyc cache files.

3. CPython VM

Python Virtual Machine executes instructions on the CPU.

2. Hello World & Formatted String Literals

In Python 3.6+, formatted string literals (f-strings) provide a fast, readable way to interpolate expressions inside strings:

python_intro.py
# Modern Python 3 Idiomatic Introduction
import sys
import platform

def greet_developer(name: str, years_exp: int) -> str:
    """Returns a tailored welcome message with runtime telemetry."""
    py_version = platform.python_version()
    status = "Senior Engineer" if years_exp >= 5 else "Rising Engineer"
    
    return f"Welcome {name}! [{status}] - Running Python {py_version}"

if __name__ == "__main__":
    user_name = "Alex"
    experience = 6
    message = greet_developer(user_name, experience)
    print(message)
    print(f"Platform interpreter: {sys.executable}")
Interactive Output
Welcome Alex! [Senior Engineer] - Running Python 3.11.4
Platform interpreter: /usr/local/bin/python3

3. Python Dynamic Typing vs Type Hinting (PEP 484)

Python is dynamically typed at runtime, but modern Python heavily embraces static type annotations for tooling, IDE auto-completion, and static analyzers such as mypy:

Feature Standard Python Modern Type-Hinted Python
Function Signature def calculate(x, y): def calculate(x: float, y: float) -> float:
Collection Hinting data = [1, 2, 3] data: list[int] = [1, 2, 3]
Nullability Implicit None from typing import Optional; val: Optional[str] = None
Important Note on the Global Interpreter Lock (GIL)

In standard CPython, the Global Interpreter Lock ensures that only one native OS thread executes Python bytecode at any single instant. For CPU-bound parallel workloads, utilize Python's multiprocessing module or concurrent process pools rather than standard threading.

Related Tags: #Python #OOP

Discussion (1)

Leave a Thought or Question
John Developer 12 days ago

Fantastic explanation of Python bytecode and the CPython execution pipeline! Really clarified how the GIL factors into CPU operations.