ALL MODULESmodule_01.py

Introduction to Computers, Programming & Python Installation

~45 minTheoryLive CodingDemoChallengeQuiz
theory.md

1. Introduction to Computer Science & The CPython Execution Engine

1.1Philosophy & The Zen of Python (PEP 20)

Python was created by Guido van Rossum in 1989 at CWI (Centrum Wiskunde & Informatica) in the Netherlands. He designed Python with one single, powerful core principle: code readability is paramount (meaning easy reading is more important than clever tricks). Why? Because as programmers, we spend far more time reading existing code to understand and fix it than we do writing brand new code!

ANALOGY TIME
Think of writing code like writing a cooking recipe. If a recipe uses secret symbols or messy handwriting, other chefs will make mistakes. But if it is written clearly with step-by-step instructions, any chef can follow it effortlessly!

The core tenets of Python are documented in a famous list called The Zen of Python (officially known as PEP 20):

  • Explicit is better than implicit: Be clear and direct about what your code is doing instead of hiding actions behind the scenes.
  • Simple is better than complex: Choose the simplest solution that works over a complicated one.
  • Readability counts: Write your code so human beings can easily read and understand it.
  • Errors should never pass silently unless explicitly silenced: If something goes wrong, the computer should speak up immediately rather than quietly ignoring the problem.

1.2Computer Hardware & Execution Paradigms

At the physical level, your computer's brain—the Central Processing Unit (CPU)—can only understand binary machine code. Binary code consists entirely of 1s and 0s (for example: 10110000 01100001), customized for specific hardware chips like Intel/AMD (x86_64), Apple/ARM (ARM64), or RISC-V.

Since writing 1s and 0s by hand is nearly impossible for humans, we write in high-level programming languages like Python. Programming languages turn human-written code into machine code using three main approaches (known as execution paradigms):

  1. Ahead-of-Time (AOT) Compiled Languages (like C, C++, Rust, Go):

- What it is: A special program called a compiler translates your entire human-written source code file into raw binary machine code all at once BEFORE you run it. - Analogy: Like translating an entire book from English into Spanish before publishing it. - Trade-off: Runs super fast, but the created executable file only works on the specific operating system and processor hardware it was built for.

  1. Interpreted Languages (like Bash, AWK):

- What it is: An interpreter reads your source code line-by-line and carries out the commands right on the spot at runtime. - Analogy: Like a live tour guide translating a spoken speech sentence-by-sentence into another language. - Trade-off: Easy to run anywhere without compiling first (highly portable), but slower because it translates as it runs.

  1. Bytecode Virtual Machine Languages (like Python, Java, C#):

- What it is: A hybrid two-step process! First, Python compiles your source code into an intermediate format called bytecode (saved in .pyc files). Next, a software program called a Virtual Machine (VM) reads and executes that bytecode step-by-step. - Analogy: Imagine writing music notes on a sheet (source code). First, it gets converted into a universal MIDI music file (bytecode). Then, a MIDI player app (the Virtual Machine) plays the song on any computer speaker!

TEXT

1.3CPython Virtual Machine Pipeline

CPython is the standard, official implementation of Python, and it is written in the C language. When you open your terminal and type python app.py, CPython runs your script through a four-stage assembly line pipeline:

  1. Tokenizer:

- Breaks down your raw text file into a stream of fundamental building blocks called tokens (such as variable names NAME, numbers NUMBER, or indentation levels INDENT and DEDENT). - Analogy: Like breaking a written sentence into individual words and punctuation marks.

  1. Parser & Abstract Syntax Tree (AST):

- Checks your tokens against Python's grammar rules to make sure there are no typos or syntax errors. It organizes these tokens into a tree structure called an Abstract Syntax Tree (ast.PyAST). - Analogy: Like checking a sentence with a grammar checker to confirm it has a subject, verb, and proper punctuation.

  1. Bytecode Compiler:

- Takes the tree structure (AST) and translates it into 16-bit virtual machine instructions called opcodes (operation codes like LOAD_CONST to load a number, STORE_FAST to save a variable, or BINARY_ADD to add two numbers).

  1. Evaluation Loop (`ceval.c`):

- A core loop inside CPython written in C (ceval.c). It reads each opcode instruction one after another and tells your physical computer CPU exactly what calculation to perform.

1.4Memory Management & The Global Interpreter Lock (GIL)

Managing computer memory (RAM) is critical so programs don't crash or run out of space. Python manages memory automatically using two key mechanisms, plus one safety rule:

  • Reference Counting (`ob_refcnt`):

- Every single item or value in Python is stored as an object (PyObject), and every object carries a counter called ob_refcnt (reference count). - Every time a new variable points to an object, its counter goes UP by 1. When a variable disappears or stops pointing to it, the counter goes DOWN by 1. - The moment ob_refcnt == 0 (zero variables are using it), Python instantly frees up that memory space for other things! - Analogy: Imagine sticking name tags onto a luggage item at an airport. As long as at least one tag is attached, the luggage stays on the carousel. As soon as the last name tag is removed (count reaches 0), the luggage handler removes the bag from the floor.

  • Generational Garbage Collection:

- Sometimes two objects point to each other in a circle (A points to B, and B points to A), even though no actual variable in your code is using them! This is called a cyclic reference. - To clean these up, Python runs a background scanner called the Garbage Collector. It tracks objects across 3 age categories called "Generations" (Generation 0 for brand new objects, Generation 1 for middle-aged ones, and Generation 2 for long-lived ones). - Analogy: Like a house cleaning routine: Gen 0 is taking out the desk trash daily, Gen 1 is weekly room cleaning, and Gen 2 is deep garage cleaning once a season.

  • The Global Interpreter Lock (GIL):

- The GIL is a master safety lock (a mutex lock) inside CPython. It ensures that only one CPU thread executes Python bytecode at any single millisecond. - Why does it exist? It prevents multiple threads from accidentally modifying memory reference counts at the exact same time, which would cause memory corruption crashes.

1.5Code Demo 1: Environment & Interpreter Metadata Inspection

Here is how we can ask Python to report technical details about its own running environment:

PYTHON

1.6Code Demo 2: Bytecode Disassembly Inspection

We can use Python's built-in dis module to look under the hood and see the exact bytecode opcodes that CPython generates for a function:

PYTHON
GOTCHA 1

Confusing the text editor (where you type code) with the terminal/interpreter (where code is executed). You must point the interpreter at your file (e.g. python my_script.py) to see output.

GOTCHA 2

Running python with no filename opens the interactive REPL (>>>). Typing code in the REPL does NOT save it to your file.

GOTCHA 3

Naming your script after standard library modules (e.g., naming your file sys.py or math.py). This breaks Python's import system because Python will import your local file instead of the built-in library module.

GOTCHA 4

On Windows, system PATH configuration may mean python points to an old version or Microsoft Store shortcut. Use py --version or verify where python in PowerShell.

WHERE THIS SHOWS UP IN REAL CODE

In production web applications, we use logs to track system health. A simple print statement in Python is the precursor to standard logging (import logging), which sends structured messages to log aggregators or cloud providers like Datadog, Grafana Loki, or AWS CloudWatch.

TRY THIS YOURSELF

Modify Demo 3 to check if sys.version_info >= (3, 10) and output a custom readiness message.

live_coding — Interactive Hello World & Diagnostic Script

Click Run to execute your code through the Pyodide WebAssembly runtime.

Python not loaded yet.
demo — Verify Python installation, check CPython version, and test print output.
  • [ ] runtime available
  • [ ] sys module importable
  • [ ] version >= 3.10
challenge_1

Welcome to your first Python coding challenge! Your goal is to write a script that outputs two friendly welcome lines.

Steps to Complete:

  1. Use a print() statement to output Hello, World! on the first line.
  2. Use a second print() statement to output Welcome to Python Programming! on the second line.

Input: None

Expected Output:

TEXT
Python not loaded yet.
challenge_2

Let's build a 3-line welcome banner that calculates hours automatically!

Steps to Complete:

  1. Print Python Studio v1.0 on line 1.
  2. Print Total Modules: 12 on line 2.
  3. Print Estimated Hours: 9 on line 3, calculating the number 9 using integer division math directly inside your print() statement: 12 * 45 // 60.

Input: None

Expected Output:

TEXT
Python not loaded yet.
challenge_3

Let's create a deployment status card for a server service!

Steps to Complete:

  1. Create variables nodes = 4 and limit = 100.
  2. Print Service: Auth-Service on line 1.
  3. Print Active Nodes: 4 on line 2.
  4. Print Total Capacity: 400 on line 3, calculating 400 by multiplying nodes * limit inside your print() statement.

Input: None

Expected Output:

TEXT
Python not loaded yet.
quiz — 6 questions
30s question timers are paused. Start individually or all together.
# question 1

What does it mean that Python is an 'interpreted' language?

# question 2

What is CPython?

# question 3

What happens when you run `python` without passing a script file name in your terminal?

# question 4

Why should you never name your script file `math.py`?

# question 5

Which built-in Python module allows you to check runtime details like `sys.version`?

# question 6

What is bytecode in Python?

discussion

# comments · intro

  • @priya · 2h ago

    The typewriter hero is such a nice touch.

    • @alex · 1h ago

      Totally agree! Made it super engaging.

  • @sam · 1d ago

    Finally a course where I can run code without leaving the page.