an ELI5 one-pager · part II

Python layer two

You've got the eight bricks. This sheet is the next course — the pieces that turn a script into something that remembers, asks, borrows tools, and doesn't fall over when something goes wrong.

09Lists, up close

Lists are numbered — but counting starts at 0, not 1. You can also tack new items onto the end.

Think of it likeA row of lockers labelled 0, 1, 2… The first locker is number 0. Confusing once, obvious forever.
crates = ["A", "B", "C"]
print(crates[0])     # -> A
crates.append("D")  # now A B C D

10Dictionaries

A dictionary stores things by name instead of by position: a key points to a value.

Think of it likeA coat check. You hand over a ticket (the key), you get back your coat (the value). No counting required.
stock = {"apples": 12, "pears": 4}
print(stock["apples"])  # -> 12
stock["pears"] = 9       # update it

11Slotting values into text

An f"..." string lets you drop variables straight into a sentence using {curly braces}.

Think of it likeA fill-in-the-blank form. You write the sentence once and Python drops the real values into the gaps.
name = "Dick"
n = 128
print(f"{name} has {n} orders")
# -> Dick has 128 orders

12Asking a question

input() pauses and waits for the person to type. It always hands back text, so wrap it in int() for numbers.

Think of it likeThe robot cupping its ear. Whatever it hears comes back as words — even "42" arrives as text until you convert it.
naam = input("Je naam? ")
leeftijd = int(input("Leeftijd? "))
print("Hallo", naam)

13Repeat until…

A while loop keeps going as long as its condition stays true — handy when you don't know the count in advance.

Think of it likeStirring the pot until it boils. Not a fixed number of stirs — you stop the moment the condition is met.
n = 3
while n > 0:
    print(n)
    n = n - 1
# -> 3, 2, 1

14Borrowing toolkits

import pulls in code other people already wrote, so you don't reinvent it.

Think of it likeBorrowing a power tool from the shed instead of whittling one by hand. Someone built it well already.
import random
pick = random.choice(crates)
print("Chosen:", pick)

15A safety net

try / except lets you attempt something risky and catch the fall instead of crashing.

Think of it likeA trapeze net. You try the trick; if it goes wrong, the net catches you and the show goes on.
try:
    total = 10 / 0
except ZeroDivisionError:
    print("kan niet delen door nul")

16Saving to a file

open() with with lets you write to (or read from) a file on disk, then tidies up after itself.

Think of it likeA filing cabinet. You open a drawer, put a note in, and it closes itself when you walk away.
with open("notes.txt", "w") as f:
    f.write("hoi Dick")
# file now holds: hoi Dick

Watch a few bricks work together

Borrow a tool, look something up by name, and slot it into a sentence — three of the ideas above in five lines:

import random

stock = {"apples": 12, "pears": 4, "plums": 7}
fruit = random.choice(list(stock))
print(f"Picking {fruit}: {stock[fruit]} left")
# -> Picking pears: 4 left