🌱 Beginner β€’ Programming

Programming Foundations

Never written code before? Start here. Learn what programs actually are, how computers follow instructions, and the core concepts that underpin all programmingβ€” no prior experience needed.

⏱ 15-18 minutes

🎯 What You'll Learn

  • What a program really is and how computers execute it
  • Variables: storing and naming information
  • Loops: repeating actions efficiently
  • Functions: reusable blocks of code
  • How to read and write pseudocode
  • The mental model for thinking like a programmer

1 What is a Program?

A program is a set of instructions that tells a computer exactly what to do. Think of it as a very detailed recipeβ€”but instead of cooking, you're telling a computer how to process information.

🍳 Recipe Analogy

Recipe: "Take 2 eggs, crack them into a bowl, whisk for 30 seconds, pour into pan, cook until solid."

Program: "Take the input number, multiply by 2, add 5, display the result."

Both are step-by-step instructions. The difference? Computers follow instructions exactly and literallyβ€”they can't improvise or assume anything.

Key Properties of Programs

  • Sequential: Instructions run one after another, in order
  • Precise: Every step must be explicitly stated
  • Deterministic: Same input always gives same output
  • Fast: Computers can execute billions of instructions per second
        A Simple Program:
        
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚  1. Get a number from the user              β”‚
        β”‚  2. Multiply the number by 2                β”‚
        β”‚  3. Add 5 to the result                     β”‚
        β”‚  4. Show the final answer                   β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        
        If user enters 10:
        Step 1: number = 10
        Step 2: 10 Γ— 2 = 20
        Step 3: 20 + 5 = 25
        Step 4: Display "25"
                    

πŸ€– Programs in AI

An AI model is essentially a very complex program. It takes input (an image, text, audio), processes it through millions of mathematical operations, and produces output (a classification, translation, or generated content). The "learning" part is just a program that adjusts the numbers (weights) based on examples.

2 Variables

A variable is a named container that stores data. It's like a labeled box where you can put information, retrieve it later, and change its contents.

        Variables as Labeled Boxes:
        
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚  age    β”‚    β”‚  name   β”‚    β”‚   score     β”‚
        │─────────│    │─────────│    │─────────────│
        β”‚   25    β”‚    β”‚ "Alice" β”‚    β”‚    98.5     β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           number        text            number
                    

Why "Variable"?

The value can varyβ€”it can change over time. That's the power: you can update the contents without changing the name.

πŸ’» Variables in Action

# Creating variables
age = 25
name = "Alice"
score = 98.5

# Using variables
print(name)       # Output: Alice
print(age + 10)   # Output: 35

# Changing a variable
age = 26          # Now age is 26
score = score + 1 # Add 1 to score (now 99.5)

Variable Naming Rules

  • Use descriptive names: user_age not x
  • No spaces: use underscores (first_name) or camelCase (firstName)
  • Can't start with numbers: 2fast ❌, fast2 βœ“
  • Case matters: Age and age are different variables

πŸ’‘ Think of Variables as...

Post-it notes with names. You can write a value on one, stick it somewhere, read it later, erase it and write something new. The label (name) stays the same, but the content can change.

πŸ€– Variables in AI

AI models have millions of variables called "weights" and "parameters." During training, these variables are continuously updated to improve the model's predictions. The learning rate, batch size, and number of epochs are also stored as variables.

3 Loops

A loop repeats a block of code multiple times. Instead of writing the same instruction 100 times, you write it once and tell the computer to repeat it.

πŸƒ Exercise Analogy

Without loops: "Do a pushup. Do a pushup. Do a pushup. Do a pushup..."
With loops: "Do 10 pushups."

Same result, much less writing!

Types of Loops

πŸ”„ For Loop (Count-based)

# Repeat 5 times
for i in range(5):
    print("Hello!")

# Output:
# Hello!
# Hello!
# Hello!
# Hello!
# Hello!

Use when you know how many times to repeat

Note: range(5) gives 0,1,2,3,4 β€” starts at 0, stops before 5

πŸ” While Loop (Condition-based)

# Repeat while condition is true
count = 0
while count < 3:
    print(count)
    count = count + 1

# Output:
# 0
# 1
# 2

Use when repeating until something changes

Loop Through Data

πŸ’» Processing a List

names = ["Alice", "Bob", "Charlie"]

for name in names:
    print("Hello, " + name)

# Output:
# Hello, Alice
# Hello, Bob
# Hello, Charlie
        Loop Execution Flow:
        
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚ Start Loop  β”‚
        β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
               β”‚
               β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    No     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚ More items? │─────────▢│  Exit Loop  β”‚
        β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚ Yes
               β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚ Do the task β”‚
        β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
               β”‚
               └──────────┐
                          β”‚ (repeat)
                          β–Ό
                   Back to "More items?"
                    

πŸ€– Loops in AI

Training a neural network is one big loop: "For each batch of data, make predictions, calculate error, update weights. Repeat for all batches. Repeat for multiple epochs." A model trained for 100 epochs has looped through the entire dataset 100 times!

4 Functions

A function is a reusable block of code that performs a specific task. You define it once, give it a name, and call it whenever you need that task done.

πŸ“± Speed Dial Analogy

Instead of dialing 555-123-4567 every time you want to call Mom, you save it as "Mom" and just tap once. A function is like that: a shortcut for a complex operation.

Function Structure

        Function Anatomy:
        
        def greet(name):           ← Name and input (parameter)
            message = "Hello, " + name
            return message         ← Output (return value)
        
        
        Using the function:
        
        result = greet("Alice")    ← Call with argument
        print(result)              ← Output: Hello, Alice
                    

Why Functions?

  • Reusability: Write once, use many times
  • Organization: Break complex tasks into smaller pieces
  • Readability: Give meaningful names to operations
  • Testing: Test each piece independently

❌ Without Functions

# Calculate area 3 times
area1 = 5 * 10
area2 = 3 * 7
area3 = 8 * 2

# What if formula changes?
# Have to update 3 places!

βœ… With Functions

def area(width, height):
    return width * height

area1 = area(5, 10)
area2 = area(3, 7)
area3 = area(8, 2)

# Change formula once,
# works everywhere!

πŸ€– Functions in AI

AI libraries are built on functions: model.fit() trains a model, model.predict() makes predictions, loss_function() calculates error. Neural networks themselves are compositions of functionsβ€”each layer is a function that transforms its input.

5 Pseudocode

Pseudocode is a way to describe an algorithm in plain language, without worrying about specific programming syntax. It's how programmers plan their code before writing it.

πŸ’‘ Why Pseudocode?

It lets you focus on the logic without getting stuck on syntax. Once your pseudocode is clear, translating it to any programming language becomes straightforward.

Example: Finding the Largest Number

πŸ“ Pseudocode

INPUT: a list of numbers

SET largest TO the first number

FOR EACH number in the list:
    IF number > largest:
        SET largest TO number

OUTPUT: largest

🐍 Python

numbers = [3, 7, 2, 9, 1]

largest = numbers[0]

for number in numbers:
    if number > largest:
        largest = number

print(largest)  # 9

Pseudocode Conventions

  • INPUT/OUTPUT: What goes in and comes out
  • SET...TO: Assign a value to a variable
  • IF...THEN: Conditional logic
  • FOR EACH: Loop through items
  • WHILE: Loop until condition changes
  • CALL: Use a function

πŸ€– Pseudocode for AI

Here's the training loop in pseudocode:

FOR each epoch:
  FOR each batch of data:
    Make predictions
    Calculate loss
    Compute gradients
    Update weights


Simple, right? The details are complex, but the structure is clear.

6 How Computers Follow Instructions

Understanding how computers execute programs helps you write better code and debug problems. Here's the simplified mental model.

The Execution Model

        Your Code          Computer's Brain         Memory
        ─────────         ───────────────         ────────
                          
        x = 5       ──▢   "Store 5 in x"    ──▢  β”‚ x: 5  β”‚
        y = 3       ──▢   "Store 3 in y"    ──▢  β”‚ y: 3  β”‚
        z = x + y   ──▢   "Get x, get y,         β”‚ z: 8  β”‚
                          add them,
                          store in z"
        print(z)    ──▢   "Get z, display"  ──▢  Output: 8
                    

Key Concepts

  • Sequential Execution: Code runs line by line, top to bottom
  • Memory: Variables are stored in memory (RAM)
  • CPU: The "brain" that executes instructions
  • State: The current values of all variables at any moment

Tracing Execution

"Tracing" means following your code step by step, tracking how variables change. This is how you debug!

πŸ’» Trace Example

# Code              State after line
x = 10              # x=10
y = 5               # x=10, y=5
x = x + y           # x=15, y=5
y = x - 3           # x=15, y=12
print(x, y)         # Output: 15 12

πŸ’‘ The Literal Computer

Computers are extremely literal. They do exactly what you say, not what you mean. If you write print("Hello") 1000 times, it will print "Hello" 1000 timesβ€”no questions asked. This is why precision in code matters!

πŸ€– Computation in AI

Training GPT-4 required massive computationβ€”estimated at 10²⁡ floating-point operations! This is just the CPU (and GPU) following instructions billions of times per second, across thousands of machines, for months. Understanding the basics helps you appreciate the scale.

🎯 Key Takeaways

  • A program is a sequence of precise instructions for a computer
  • Variables store data with namesβ€”like labeled boxes
  • Loops repeat code efficientlyβ€”essential for processing data
  • Functions are reusable code blocksβ€”the building blocks of programs
  • Pseudocode lets you plan logic before worrying about syntax
  • Computers execute code literally and sequentially

What's Next?

Now that you understand the concepts, it's time to see them in action with Python: