The glue language of your server: how scripts run, the data they hold, and the patterns you'll recognize in every line of backend code.
← / → to navigate · EN/VI switch below
Everything behind your frontend — the app, the tooling, the scripts — is usually Python. You'll read it, judge it, and steer the AI that writes it.
Flask — the framework this course ships with — is a Python library. Every page your app serves is decided by Python code.
Python connects the pieces: takes a web request, reads data, calls other tools, writes files. It's the connective tissue of a backend.
Python is among the languages AI generates best. You don't type it from memory — you ask, get code, and check it.
This deck gives you the mental model: what each construct is for. That's what lets you review AI code and nudge it in the right direction.
AI writes the Python. Your skill is the three moves that make AI code trustworthy.
You don't type from memory. You look at code — yours or the AI's — and know what each block is for: a function, a loop, a data read.
Any line you can't recognize → paste it to the AI: 'what does this do?' Explanations are free; recognition is the skill.
The best check is running it: print what you expect, read the result, compare. Output is the truth — not the code, not the AI's confidence.
Two doors into the language — and what happens when things break.
Variables store values in labelled boxes; print() shows them. That's the entire vocabulary of a beginner script.
# hi.py — a comment: Python ignores it, humans read it
name = "shipper" # a variable: a labelled box
print("Hello, " + name) # send text to the screen
print("Ready to build.")
That's a complete program: a variable and two print calls. Nothing else is required to start.
A variable is a name pointing at a value. The name is the label; the value (and its type) can change.
score = 82 — the name score now points at the value 82. Read it as 'score holds 82'.
score = 90 later just moves the label to a new value. Boxes aren't fixed — labels are what move.
You don't declare types. 82 is an int, "An" is a str, True is a bool — Python knows from the value itself.
One command runs a whole file; the same command with no file opens the REPL (Read-Eval-Print Loop) — a live playground for testing one line at a time.
Every Python error ends in a traceback: a stack of where you were, and the actual problem on the last line.
You'll paste these into the AI constantly. Knowing the three parts makes you fast at it.
File "hi.py", line 4 tells you exactly where to look. That's your pointer for targeted tweaks.
NameError (typo / undefined), KeyError (missing dict key), TypeError (wrong type). The name is a category, not a verdict.
Copy the whole traceback, not just the last line. The stack above shows the path that led there.
A program is mostly data in motion. Five small types cover nearly everything — and two of them (list, dict) are the stars.
You'll meet these in every backend. str/int/float/bool are the atoms; list & dict are how data is actually organized.
| Type | Example | What it's for |
|---|---|---|
| str | "hello" | Text: names, messages, URLs |
| int | 42 | Whole numbers: counts, ids, ports |
| float | 3.14 | Decimal numbers: prices, scores |
| bool | True / False | A yes/no switch: is_active, is_admin |
| list | ["a", "b"] | An ordered set of things — tasks, results, users |
| dict | {"name": "An"} | Label → value pairs — one record: a user, a request, a config |
Every API response, config file and database row arrives as JSON. Python's list and dict read it natively — no translation layer.
# A list: ordered things
tasks = ["design", "code", "test", "ship"]
# A dict: labelled values (a record)
user = {
"name": "An",
"role": "designer",
"active": True,
}
If you've seen JSON, you already know this: dict = the JSON object {…}, list = the JSON array […].
One is a verb, the other is a question. Confusing them is one of the most common things to catch in AI (and human) code.
Put a value in a box: name = "An". The right side is computed first, then stored. It changes state.
Ask a question: name == "An" → True or False. It changes nothing — it just answers, and the answer is a bool.
Strings are the most common data in a web app. A handful of operations cover the daily work.
len("hello") → 5. Counts the characters.
"ell" in "hello" → True. Substring check; it works on lists too (next slide).
"An".upper() → "AN". Case changes for matching and display.
"a b c".split() → ["a", "b", "c"]. Text in, list out — the text-to-data bridge.
Data isn't only created — you reach into it. These three moves show up in almost every loop you'll ever read.
tasks = ["design", "code", "ship"]
print(len(tasks)) # 3
print(tasks[0]) # design — counting starts at 0
print("design" in tasks) # True
age = 29
print(age > 18) # True ← a comparison makes a bool
print(age == 30) # False
Counting starts at 0, so the first item is index 0 and the last is len − 1. Comparisons (>, <, ==) don't change anything — they produce True/False.
Between values and errors, Python has a third state: a legitimate absence.
Python's word for 'nothing here'. A function without return gives None; a missing .get() gives None.
It's honest: 'I looked, there's nothing.' Better than crashing, better than faking a value like 0 or "".
if value is None: — the standard way to ask 'did we get anything?' before using it.
How Python decides and repeats — the shapes you'll recognize in any snippet AI hands you.
Don't memorize syntax — recognize the shape: a condition, a colon, an indented block.
score = 82
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "C"
print(grade) # B
Python checks top to bottom and runs the first branch whose condition holds. else catches everything left. Indentation is the grouping — no braces.
The workhorse loop: give it any collection, it hands you the items one at a time.
for task in ["design", "code", "ship"]:
print(task)
for i in range(3): # range(3) → 0, 1, 2
print(i)
for walks any collection, one item at a time — a list, or the range(n) 'count from 0' collection. Most loops in real code are for-loops.
The loop you must control: it runs as long as the condition stays true.
retries = 3
while retries > 0:
print("retrying...")
retries = retries - 1 # runs 3 times
You must move the condition toward false inside the loop. If you forget — infinite loop, and the server stops answering. for handles most jobs; while is the careful one.
Real conditions are rarely a single comparison. Three words combine them into English-like tests.
age >= 18 and active — both sides true, or the whole thing is false. It means 'also'.
role == "admin" or role == "owner" — at least one true. It means 'either'.
not banned — true when banned is false. It means 'not'. You'll see these three in nearly every if you'll ever review.
Functions, strings, data operations, files, imports — the daily toolkit of a backend.
A function packages a task: name it, give it inputs, get a result back. Call it as often as you like.
def greet(name): # define: a reusable recipe
return f"Hello, {name}!"
print(greet("An")) # Hello, An! ← call it
print(greet("You")) # Hello, You! ← call again
def names the recipe, arguments (name) are the inputs, return hands back the result. The f"…" string inside is the next slide.
The two smallest but most visible constructs — you'll spot them on line one of almost every file.
f"Hi {name}, {n} tasks left" — values drop straight into the text. The #1 way to build messages.
f"{2 + 3}" gives "5". Functions work too: f"{len(tasks)}".
Python skips everything after #. AI reads them too — a good comment is a steering wheel for the AI that maintains your code.
Three moves cover most of the daily work: grow a list, read a dict safely, loop over things.
tasks = ["design", "code"]
tasks.append("ship") # grow the list
user = {"name": "An", "role": "designer"}
print(user["name"]) # An
print(user.get("email")) # None — safe if missing
for t in tasks: # loop over items
print(t)
d["key"] crashes if the key is missing; d.get("key") calmly returns None. Prefer .get() when the key is uncertain — most AI bugs come from missing keys.
Six operations cover the daily work. All of them are methods: data.method().
| Operation | What it does |
|---|---|
| len(x) | Length of a list, dict or string. |
| x in data | Membership test — is this item or value inside? → True/False. |
| data.append(v) | Add v to the end of a list. Grows it. |
| d.get(k) | Read a dict value; None instead of a crash if the key is missing. |
| d.keys() | The list of a dict's labels. How you loop over a record. |
| d.items() | Label + value pairs, for looping over both at once. |
Files are how a program remembers things between runs — configs, data, logs. The shape below appears in almost every backend you'll touch.
with open("tasks.txt") as f: # open, and close for you
for line in f: # a file is a list of lines
print(line.strip()) # remove the newline
open() hands you a file handle; with guarantees it's closed. Line by line, a file is just a list of strings.
Same open/with shape as reading; the only difference is one letter: the mode.
with open("out.txt", "w") as f: # w = write, erases first
f.write("task: design\n")
with open("out.txt", "a") as f: # a = append, keeps existing
f.write("task: ship\n")
w starts fresh, a adds on. This is exactly how apps write logs and save data — the same shape you've just learned for reading.
You don't build everything yourself. One line brings in a whole toolbox — Python ships with a huge standard library.
import json gives you JSON tools; import math gives you math. One line, whole toolbox — the top of nearly every file.
json.loads turns a JSON string into dict/list; json.dumps does the opposite. This is how your app talks to the web.
from flask import Flask — even 'using Flask' is just an import. Every backend you'll read starts with imports.
Real data is messy: missing keys, empty files, bad input. Programs that handle it keep running.
try:
value = user["email"]
except KeyError:
value = "no email"
print(value)
try: attempt the risky part; except: if it fails, do this instead. Programs degrade gracefully instead of crashing — you'll see this around every data read.
Nothing new here — just the deck's pieces in one place. Read it top to bottom; you already know every line's job.
import json
def summary(raw):
users = json.loads(raw) # JSON string -> list of dicts
active = 0
for u in users:
if u.get("active"):
active = active + 1
return f"{active} of {len(users)} active"
raw = '[{"name": "An", "active": True}]'
print(summary(raw)) # 1 of 1 active
import, def, json.loads, for, if, .get, len, f-string — the whole vocabulary of this deck, doing real work in twelve lines.
Everything was preparation for this: your Flask app, the tooling, the scripts — all of it is the pieces you just learned.
Now look at this course's own app: a .py file with functions, dicts, files and JSON. Every piece on this slide is in that app.
"GET /slides/python-basics" → Python calls a function → it returns a page. def + return, exactly the greet() slide.
Every web request arrives as a labelled bag: query params, form fields, headers. You read it with d.get("key") — the everyday-moves slide.
The app reads data (files, database), shapes it into dicts and lists, sends it to your browser as JSON — the list/dict from Part 2.
Settings and data come from files — the open/with shape from Part 4. Python is the connective tissue between the web and the disk.
The daily loop of this course: you and the AI, one code block at a time.
Before asking anything, look for the patterns: def, for, if, dict, open. You already know what each one is for.
Paste a block and ask 'explain this in simple words.' Compare the AI's answer with your own read — that gap is where learning happens.
Tweaks work best with a target: 'change this function to also check the email key.' Specifics in, specific changes out.
After any change, run it and look at what it prints or serves. Output right → shape right. Output wrong → paste it back and repeat.
You don't need to write Python by hand. You need to recognize it, judge it, and steer the AI that writes it.
Next: your first Flask route — the same Python, now answering the web. Ask the AI to explain any line you don't recognize.