# The line starting with # is a comment. Python ignores it
print("Welcome to your food-delivery startup!")
print("Stickers bought:", 1200) # commas print several things, a space betweenWelcome to your food-delivery startup!
Stickers bought: 1200
Programming with Python
. . .
I really appreciate active participation and interaction!
. . .
Materials live in the KLU portal and are hosted at python.tobiasvlcek.com.
.py file and upload it to Moodle. . .
The weekly lab exercises rehearse exactly this routine. Do them, and the checkpoints will feel familiar.
. . .
Learning the basics without AI first is what lets you judge AI output later.
uv) and the Zed editor on your own laptop, with our guides; Session X then runs on your machineCmd/Ctrl+Enter (or the play button at its edge). The output appears right below it# YOUR CODE BELOW; the check under each exercise updates by itselfCmd/Ctrl+S, then menu (top right) → Download → Download Python code. The tab remembers you until you close it; the download is forever. . .
Why now? From Session VI you get Zed’s paid editor plan for free as a student, and Zed only accepts GitHub accounts that are at least 30 days old. In Session X your project lives on GitHub. Nothing else to install until Session IX.
This semester you are founding a campus food-delivery startup. Today it gets a name, a menu, and its first revenue calculation.
. . .
(Your co-founder Tobi has already spent the marketing budget on stickers.)
. . .
Today your startup’s first program does three small things: record the founding data, do one calculation, and print a summary.
print()print() is a function: you hand it something, it shows it on screen.
# The line starting with # is a comment. Python ignores it
print("Welcome to your food-delivery startup!")
print("Stickers bought:", 1200) # commas print several things, a space betweenWelcome to your food-delivery startup!
Stickers bought: 1200
=: name on the left, value on the rightcompany_founded = 2026 # store a whole number under a name
print(company_founded)2026
Store the facts of the new company, then use them by name:
company_founded = 2026
first_employee = "Tobi" # co-founder, but he put himself on the payroll first
sticker_budget = 300 # what's left after Tobi's spending spree
print(first_employee)
print(sticker_budget)Tobi
300
Tobi spends another 50 EUR of the budget. What does the last line print?
sticker_budget = 300
sticker_budget = sticker_budget - 50
print(sticker_budget)a) Error b) 300 c) 250
. . .
Predict first. Pick a letter, then I reveal the answer.
c) 250: the right side is computed first (300 - 50), then the name is pointed at the new value. A name always shows its latest value.
sticker_budget = 300
sticker_budget = sticker_budget - 50
print(sticker_budget)250
_budget and Budget are two different namesfor, if, def, …). . .
Question (we solve this together, out loud): Which of these are valid variable names?
sticker_budget, 1st_employee, firstEmployee, company-name, _tobi
Tobi’s grand plan: sell everything for 9.99. To see whether that survives contact with arithmetic, we need Python’s operators.
# margin = price - cost
print(9.99 - 7.40)2.59
print(9.99 + 1.50) # addition
print(9.99 - 7.40) # subtraction
print(9.99 * 3) # multiplication
print(300 / 8) # division, always gives a float11.49
2.59
29.97
37.5
// and %: how many fit, what’s left// floor division: how many whole times something fits% modulo: the remainder left over# The 300 EUR budget, boxes at 7 EUR each
print(300 // 7) # whole boxes we can buy
print(300 % 7) # EUR left over
print(10 // 2.5) # 4.0: floor division on floats floors, but returns a float42
6
4.0
* and / happen before + and -. Parentheses override that.
print(2 + 3 * 4) # 14, not 20
print((2 + 3) * 4) # 2014
20
Tobi buys 12 boxes at 25 EUR from the 300 EUR budget. What prints?
print(300 - 12 * 25)a) 0 b) 7200 c) Error
. . .
Predict first. Pick a letter, then I reveal the answer.
300 - 12 * 25a) 0: 12 * 25 = 300 happens first, so the budget is wiped out. Reading left to right would give 7200, but Python follows precedence, not reading order.
print(300 - 12 * 25)0
Two friends split a 10 EUR order. What prints?
print(10 / 2)a) 5.0 b) 5 c) Error
. . .
Predict first. Pick a letter, then I reveal the answer.
10 / 2a) 5.0: / always returns a float, even when the division comes out even. For a whole number, use //.
print(10 / 2)5.0
int + int stays an int; / always produces a floatint and a float gives a floatmargin = 9.99 - 7.40
print(margin)
print(type(margin))2.59
<class 'float'>
round(): clean moneyDivision can leave a long tail of decimals. round(value, 2) keeps two. Cents.
# Split a 10 EUR order three ways
print(10 / 3) # 3.3333333333333335, ugly
print(round(10 / 3, 2)) # 3.33, a proper amount3.3333333333333335
3.33
A string is text in quotes. Single or double quotes both work, just be consistent.
item = "Miso Ramen"
print(item)
print('Tobi shouts "9.99!"') # mix quotes to include a quote insideMiso Ramen
Tobi shouts "9.99!"
You can glue strings with +, but numbers must be converted first, and it gets clumsy fast:
qty = 2
item = "Miso Ramen"
total = 23.00
print(str(qty) + "x " + item + ": " + str(total) + " EUR")2x Miso Ramen: 23.0 EUR
. . .
(str(...) turns a value into text, which is clumsy. We’re about to see the better way.) Forget one str() and Python raises a TypeError.
Tobi stored two order quantities as text and adds them up. What prints?
print("3" + "4")a) Error b) 34 c) 7
. . .
Predict first. Pick a letter, then I reveal the answer.
"3" + "4"b) 34: with quotes, these are strings, and + on strings glues them together. To add numbers, drop the quotes (or convert with int()).
print("3" + "4")34
Put an f before the quote and write values in { }, and Python fills them in:
qty = 2
item = "Miso Ramen"
total = 23.00
print(f"{qty}x {item}: {total} EUR")2x Miso Ramen: 23.0 EUR
. . .
No str(), no +. Just the sentence you want.
. . .
Download your .py before you leave. Closing the tab without downloading loses your work, and downloading is exactly how you hand in the checkpoints.
str, int, float, bool)* and / before + and -; round(x, 2) for money{value:.2f} turn numbers into clean receipt lines. . .
Next time, Episode 2: the city bans delivery after 22:00, and Tobi suggests “we just deliver yesterday’s orders.” We’ll need conditionals and loops to survive it. Plus an ungraded 15-minute practice checkpoint before the lab.
. . .
Think Python is a great book to start with and is free online. Schrödinger programmiert Python is a playful German-language alternative with lots of examples.
. . .
For more, see the literature list of this course.