Programming with Python
Kühne Logistics University Hamburg - Fall 2026
The first 40 minutes are the checkpoint. It starts now.
When you’re done: menu → Download → Download Python code → upload the .py to the “Checkpoint 1” assignment on Moodle.
The green ✅ live checks are provisional. The final grading runs on our side. And take a breath: everything in it was rehearsed in the labs.
Pens down. The checkpoint is behind you. Now the reason we’re all here.
Kevin has been “reusing” code the only way he knows how: he pasted the same receipt block 14 times, once per menu item. A ten-cent price change last week cost him a whole afternoon of hunting down copies.
Today the code learns to reuse itself. We meet the function, and build a first class.
Kevin totals each receipt line by hand, the same shape, over and over:
9.0
12.0
16.0
Same calculation, retyped every time. Change the rule once and you must fix it everywhere. There is a better way.
def: name the calculation onceWrite the calculation once, give it a name, and call it whenever you need it:
9.0
def starts the definition · the name is line_total · the block is indented · return hands the answer back.
qty, price2, 4.509.6
Same function, different arguments, different result. No retyping.
return: hand the value backreturn sends a value back to whoever called the function, so you can store it and use it later:
9.6
11.1
label_price prints but has no return. What does the last line show?
a) 8.50 EUR b) Error c) None
Predict first. Commit to an answer before the next slide.
print shows, return hands backc) None — label_price prints its line while running, but with no return it hands back None. That None is what lands in result. Printing is not returning.
8.50 EUR
None
A parameter can carry a default, used when the caller leaves it out:
4.0
8.0
Defaults let one function cover the common case and the special case.
A function can use another function you already wrote — reuse builds on reuse:
9.45
line_total and service_fee do their jobs; bill just orchestrates. That’s how small pieces become a program.
Open the exercise (scan the QR or type the link):
beyondsimulations.github.io/Introduction-to-Python/notebooks/ex_03_a/
First predict what happens, then run it.
A parameter is the function’s own private copy. What happens inside stays inside:
13
Names created inside a function live in its scope. They vanish when the function ends.
bump adds ten to its parameter. We call it, then print stock. What does the last line print?
a) 3 b) 13 c) Error
Predict first. Commit to an answer before the next slide.
a) 3 — bump changes only its own copy n. We never stored what it returned, so stock out here never moves. The function cannot reach out and rewrite your variables.
3
stock = bump(stock)). Deliberate, visibleIsolation is what makes functions safe to reuse. That is the whole point of this episode.
Sometimes data and the things you do with it belong together. A class is a blueprint that bundles both:
A method is just a function that lives inside a class and can read the object’s own data.
class, __init__, and selfclass Delivery:
def __init__(self, courier, distance_km): # runs when you build one
self.courier = courier # store data ON the object
self.distance_km = distance_km
self.rate_per_km = 1.20 # every delivery carries its rate
def fee(self): # a method — note self
return round(self.distance_km * self.rate_per_km, 2)__init__ sets up a new object · self is this object · attributes are stored on self and read back through self. fee() multiplies two stored attributes.
Instantiate the class to make an object, then use its data and its methods:
Nadia
4.8
trip is one Delivery object. trip.fee() computes from its own stored distance: data and behavior, traveling together.
__init__ and one method. That’s itself confusion), and that is not this courseIf the self keyword feels odd right now, that’s completely normal. Copy the shape from the worked example. The intuition follows the practice.
Open the exercise (scan the QR or type the link):
beyondsimulations.github.io/Introduction-to-Python/notebooks/ex_03_b/
First predict what happens, then run it.
return, give a parameter a default, and build the startup’s first Order class, ending in the Tip Calculator ChampionshipDownload your .py before you leave. Closing the tab without downloading loses your work, and downloading is exactly how you just handed in the checkpoint.
def, takes parameters, and hands a value back with return: print shows, return gives back (no return → None)self, via __init__) with behavior (methods). You’ll write just one small one todayNext time — Episode 4: the menu outgrows Kevin’s seventeen loose variables (price1, price2, price_final_FINAL2), and nobody can find anything. We give the data a shape: lists and dictionaries.
Nothing new here, but these are still great books to start with!
For more, see the literature list of this course.
Lecture III - Building Reusable Functions | Dr. Tobias Vlćek | Home