The Building Blocks

Every program — no matter how complex — is built from a small set of primitives. Master these and you can read and write code in any language.


1. Variables

A variable is a named container for a value. The name stays the same; the value can change.

python
Loading...

Types you'll use constantly:

TypeExamplesUsed for
Integer0, -3, 1000Counts, indices
Float3.14, -0.5Decimals, measurements
String"hello", "42"Text
Booleantrue / falseFlags, conditions

2. Conditionals

Conditionals let the program make decisions.

python
Loading...

The program picks exactly one branch to execute, based on which condition is true first.


3. Loops

Loops repeat a block of code.

For loop — iterate a known number of times or over a collection:

python
Loading...

While loop — repeat as long as a condition holds:

python
Loading...

4. Functions

A function is a reusable named block of code. Give it a name, define what inputs it takes (parameters), and what it produces (return value).

python
Loading...

Functions keep code DRY (Don't Repeat Yourself). Instead of writing the same logic three times in three places, write it once as a function.


5. Arrays / Lists

An array stores an ordered collection of values. Access any element by its index (0-based).

python
Loading...
OperationTime
Read by indexO(1)
Append to endO(1) amortized
Insert at frontO(N) (everything shifts)
SearchO(N)

Code Examples in All Languages

Loading...

6. Putting It Together — FizzBuzz

The classic "can you code?" problem. Print numbers 1–20; for multiples of 3 print "Fizz", multiples of 5 print "Buzz", both print "FizzBuzz".

python
Loading...

This one program uses all five concepts: variables (i), conditionals (if/elif/else), a loop (for), the modulo operator as a function-like tool, and implicitly demonstrates the value of readable code.


Key Takeaway

Every algorithm you'll study — sorting, searching, trees, graphs — is composed of exactly these primitives: variables hold state, conditionals branch logic, loops repeat work, functions encapsulate behaviour, and arrays store collections. Learning algorithms is learning how to combine these pieces cleverly.