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.
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: "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.
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"
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.
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
The value can varyβit can change over time. That's the power: you can update the contents without changing the name.
# 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)
user_age not xfirst_name) or camelCase (firstName)2fast β, fast2 βAge and age are different variablesPost-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.
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.
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.
Without loops: "Do a pushup. Do a pushup. Do a pushup. Do a pushup..."
With loops: "Do 10 pushups."
Same result, much less writing!
# 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
# 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
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?"
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!
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.
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 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
# Calculate area 3 times area1 = 5 * 10 area2 = 3 * 7 area3 = 8 * 2 # What if formula changes? # Have to update 3 places!
def area(width, height): return width * height area1 = area(5, 10) area2 = area(3, 7) area3 = area(8, 2) # Change formula once, # works everywhere!
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.
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.
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.
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
numbers = [3, 7, 2, 9, 1] largest = numbers[0] for number in numbers: if number > largest: largest = number print(largest) # 9
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.
Understanding how computers execute programs helps you write better code and debug problems. Here's the simplified mental 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
"Tracing" means following your code step by step, tracking how variables change. This is how you debug!
# 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
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!
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.
Now that you understand the concepts, it's time to see them in action with Python: