Step 0 / 8

The Composition of Code

How small pieces combine to build entire programs — step by step.



Level 1

Expression

The smallest unit that produces a value. Combine variables and operators to create an expression.

price + * + tax
= price * tax
Arithmetic
a + b * 2
Comparison
age >= 18
Function Call
len("hello")

Check Your Understanding

1. Which of these is an expression?

x = 10
x + y * 2
if x > 5:

2. What does an expression always produce?

A side effect
An error
A value
Level 2

Statement

A statement is a complete instruction that performs an action. An expression becomes a statement when it does something — assigns, declares, calls, or controls.

expression + action
= statement
Assignment Statement
total = price * tax
Declaration Statement
int count = 0
Function Call Statement
print("Hello World")
Return Statement
return result
Import Statement
import math

Check Your Understanding

1. Which is NOT a type of statement?

x = 5 (assignment)
print("hi") (call)
a + b (bare expression)

2. What turns an expression into a statement?

Adding parentheses
Performing an action (assign, declare, call, return)
Adding a semicolon
Level 3

Block

A block is a group of statements that execute together sequentially. Indentation or braces define a block's boundary.

stmt 1 + stmt 2 + stmt 3
= { block }
Python Block (indentation)
    x = 10
    y = x * 2
    print(y)
Java Block (braces)
{
    int x = 10;
    int y = x * 2;
    System.out.println(y);
}

Check Your Understanding

1. What defines the boundaries of a block?

Indentation (Python) or braces (Java/C)
Blank lines between statements
Comments in the code

2. Can a block contain just one statement?

No, it needs at least two
Yes, even a single statement can form a block
Level 4

Control Flow

Control flow structures decide which blocks run and how many times. They give your code decision-making and repetition.

condition + block
if...else for while
If / Else — decision
if age >= 18:
    print("Adult")
else:
    print("Minor")
For — iteration
for i in range(5):
    print(i)
While — conditional loop
while count < 10:
    count += 1

Check Your Understanding

1. What two things does a control flow structure need?

A variable and operator
A function and a class
A condition and a block

2. What's the difference between for and while?

for is faster than while
for iterates a known range; while runs until a condition flips
They are identical
Level 5

Function

A function is a named, reusable block with parameters (inputs) and a return value (output). It doesn't need read/print — many functions do pure computation.

name + params + block + return
= def function()
Pure computation (no I/O)
def area(radius):
    return 3.14 * radius ** 2
With control flow inside
def grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    else:
        return "C"

Check Your Understanding

1. Does every function need print() or input()?

Yes, that's how functions communicate
No, many functions use only params and return

2. What makes a function different from a plain block?

It has a name, parameters, and can be reused
It runs faster
It must contain a loop
Level 6

Class

A class bundles related functions (methods) and data (attributes) into a single blueprint. Objects are created from classes.

methods + data
= class Blueprint
Class Example
class BankAccount:
    def __init__(self, owner, balance):
        self.owner = owner      # data
        self.balance = balance  # data

    def deposit(self, amount):  # method
        self.balance += amount

    def withdraw(self, amount): # method
        self.balance -= amount

Check Your Understanding

1. What two things does a class combine?

Loops and conditions
Files and folders
Data (attributes) and functions (methods)

2. What is an "object"?

Another word for a class
A specific instance created from a class
A type of variable
Level 7

Module

A module is a single file containing related classes, functions, and variables. It organizes code by responsibility.

Class A + Class B + helpers
= module.py
Module: auth.py
# auth.py — one file, one responsibility
class User: ...
class Admin(User): ...

def hash_password(pw): ...
def verify_token(token): ...
Importing a module
import auth
user = auth.User("Alice")

Check Your Understanding

1. What is a module in most languages?

A folder of files
A single file with related code
A running program

2. Why organize code into modules?

Separation of concerns — each file handles one job
To make the code run faster
Level 8

Package

A package is a collection of modules (a folder) that can be distributed and reused. Packages can contain sub-packages too!

auth.py + models.py + utils.py
= 📦 mypackage/
Package folder structure
mypackage/
├── __init__.py
├── auth.py        # module
├── models.py      # module
├── utils.py       # module
└── payments/      # sub-package
    ├── __init__.py
    └── stripe.py
Using a package
from mypackage.auth import User
from mypackage.payments.stripe import charge

Check Your Understanding

1. What is the relationship between modules and packages?

They are the same thing
A package contains multiple modules
A module contains multiple packages

2. What real-world examples are packages?

print() and len()
Variables like x = 5
numpy, django, react
🏗️

You Built the Full Picture!

From the tiniest variable to a full package — every level composes the next.