Python is a way of writing instructions for a very literal, patient robot. It reads your lines top to bottom and does exactly what they say — no more, no less. Here are the bricks you build everything from.
print() is how Python speaks. Whatever you put in the brackets, it shows on screen.
print("Hoi, Dick!")
A variable is a labelled jar. You put something in it and stick a name on the front, then reuse the name later.
name = "Dick" age = 42 print(name) # -> Dick
Jars hold different types: whole numbers, text ("strings"), true/false, and lists of things.
price = 3.5 # number label = "pallet" # string (text) in_stock = True # true / false crates = ["A", "B", "C"] # list
Anything after a # is a comment. Python ignores it completely — it's just for humans.
# count today's orders orders = 128
if / else lets Python choose a path depending on whether something is true.
if age >= 18: print("Welkom") else: print("Sorry, nee")
A for loop does the same step once for every item in a list.
for crate in crates: print("Scanning", crate) # -> Scanning A, B, C
A def bundles steps under one name. Write it once, call it whenever you need it.
def greet(who): print("Hallo", who) greet("Philip") # -> Hallo Philip
Python uses spacing to show what belongs inside what. The indented lines are "inside" the line above.
if in_stock: print("ship it") # inside the if print("done") # outside — always runs
1 Put your code in a file ending in .py, e.g. hello.py.
2 Open Terminal in that folder and type python3 hello.py.
3 The robot reads top to bottom and does exactly what you wrote. That's the whole game.