Week 1 · Python and your developer toolkit
Live session: Python basics
Session outline and demo code for the first live session, from variables to loops.
Session details
- When and where: see the calendar invite for the time and video link
- Bring: a working setup from the setup guide, and your
~/projects/fde-coursefolder from the terminal lesson's daily task - How to follow along: type every demo yourself in a file called
basics.pyand run it withpython3 basics.py. Don't copy and paste.
Agenda
| Time | Segment | Format |
|---|---|---|
| 0:00 | Check-in: setup problems and questions from the pre-work | Whole group |
| 0:10 | Demo 1: values, variables and types | Live coding |
| 0:30 | Demo 2: working with text | Live coding |
| 0:45 | Demo 3: lists and dictionaries | Live coding |
| 1:05 | Break | |
| 1:15 | Demo 4: making decisions with if | Live coding |
| 1:30 | Demo 5: repeating work with loops | Live coding |
| 1:50 | Reading errors, and preview of the guided build | Instructor |
Demo 1: values, variables and types
A variable is a name that points to a value. You create one with =:
customer = "Noor Foods"
order_total = 12800
discount_rate = 0.1
is_paid = False
Every value has a type, which decides what you can do with it:
| Type | Example | Used for |
|---|---|---|
str (string) | "Noor Foods" | Text |
int (integer) | 12800 | Whole numbers |
float | 0.1 | Numbers with a decimal point |
bool (boolean) | True, False | Yes/no values |
Check a value's type with type(), and do arithmetic with +, -, *, /:
print(type(order_total)) # <class 'int'>
discount = order_total * discount_rate
final_total = order_total - discount
print(final_total) # 11520.0
Lines starting with # are comments: notes for humans that Python ignores.
Demo 2: working with text
Strings have built-in tools, called methods, that you call with a dot:
raw_email = " Support@NoorFoods.PK "
email = raw_email.strip().lower()
print(email) # support@noorfoods.pk
print(len(email)) # 20
To put values inside text, use an f-string: put f before the quotes and the values in curly braces:
print(f"{customer} owes Rs {final_total:,.0f}")
# Noor Foods owes Rs 11,520
The :,.0f part formats the number with commas and no decimal places.
Demo 3: lists and dictionaries
A list holds several values in order. Positions start at 0:
order_totals = [4500, 12800, 2300]
print(order_totals[0]) # 4500
order_totals.append(7600)
print(len(order_totals)) # 4
print(sum(order_totals)) # 27200
A dictionary stores values under names, called keys. Think of one row of a spreadsheet:
order = {"id": "A102", "customer": "Noor Foods", "total": 12800, "status": "pending"}
print(order["customer"]) # Noor Foods
order["status"] = "delivered"
print(order)
Real data is often a list of dictionaries: many rows, each with the same keys. You'll use this shape all the time, including when you read data from APIs in week 3.
Demo 4: making decisions with if
total = 12800
if total > 10000:
print("Large order: check stock before confirming")
elif total > 5000:
print("Medium order")
else:
print("Small order")
The indented lines belong to the if above them. Python uses indentation (4 spaces) to group code, so it matters. Comparisons you'll use: == (equal), != (not equal), >, <, >=, <=. Combine conditions with and and or.
Demo 5: repeating work with loops
A for loop runs the same code for each item in a list:
orders = [
{"id": "A101", "total": 4500, "status": "delivered"},
{"id": "A102", "total": 12800, "status": "pending"},
{"id": "A103", "total": 2300, "status": "delivered"},
]
pending_count = 0
for order in orders:
if order["status"] == "pending":
pending_count += 1
print(f"Pending: {order['id']}")
print(f"{pending_count} order(s) pending")
pending_count += 1 is short for pending_count = pending_count + 1. Starting a variable at zero and adding to it inside a loop is called accumulating, and it's one of the most common patterns in programming.
Reading errors
You'll see these four errors constantly this week. Each message tells you the line number and what went wrong:
| Error | Usual cause |
|---|---|
SyntaxError | A typo in the code's structure: a missing quote, bracket or colon |
IndentationError | Lines inside an if or for aren't indented consistently |
NameError | You used a variable before creating it, or misspelled its name |
TypeError | You mixed types that don't work together, such as "5" + 5 |
After the session
- Finish the daily task above.
- Make sure your
~/projects/fde-coursefolder is ready for the guided build. - Write down any error you couldn't fix, with the exact message, and bring it to the next session.
Finished this lesson?