What’s one thing you learned? What’s still confusing?
Lists, Tuples & Sets
Python's core sequences — lists, tuples, sets, frozensets — with time complexity of every operation.
List Comprehensions: Python's Superpower
Write concise, fast list/dict/set transformations in one readable line.
Mini-Project: Grade Statistics
Compute a class report (mean, min, max, pass rate, top scorers) using list comprehensions.
Interactive Labs for This Track
Loop Visualizer
You're a factory robot repeating the same task on an assembly line — watch how loops automate repetitive work
List Slicing
You have a playlist of 50 songs — grab just tracks 10 through 20 with a single slice expression
Sorting Algorithms
You're organizing a library of 10,000 books — which sorting method is fastest?
Ask questions, share insights
A function-based tip calculator. The user enters a bill amount, a tip percent, and the number of people splitting the bill, and the program prints how much each person owes. The logic lives in clean, testable functions you can reuse from any other script.
Bill amount: 84.50
Tip percent (default 15): 18
Number of people (default 1): 3
Subtotal: $84.50
Tip (18%): $15.21
Total: $99.71
Per person: $33.24
Start with the simplest possible version: a function that takes a bill amount and a tip percent, and returns the tip amount in dollars.
def calculate_tip(bill, tip_percent):
return bill * tip_percent / 100
print(calculate_tip(100, 15)) # 15.0
print(calculate_tip(84.50, 18)) # 15.21return, not print. That means the caller decides what to do with the result. Second, the function is "pure" — given the same inputs, you get the same output, every time.What does this print? def tip(bill): bill * 0.15 result = tip(100) print(result)
What's the difference between `return x` and `print(x)` inside a function?
Most people tip somewhere around 15%. Make that the default so callers don't have to specify it every time.
def calculate_tip(bill, tip_percent=15):
return bill * tip_percent / 100
print(calculate_tip(100)) # 15.0 -- used the default
print(calculate_tip(100, 20)) # 20.0 -- overrode the defaultdef f(x=1, y) — Python will complain.Which call computes the tip on a $50 bill at 20%? def tip(bill, percent=15): return bill * percent / 100
def calculate_check(bill, tip_percent=15):
tip = bill * tip_percent / 100
total = bill + tip
return tip, total
tip, total = calculate_check(84.50, 18)
print(f"Tip: ${tip:.2f}")
print(f"Total: ${total:.2f}")return a, b packs both values into a tuple. The caller unpacks them with tip, total = .... The :.2f format spec rounds to two decimal places — perfect for currency.What gets printed? def stats(): return 10, 20, 30 result = stats() print(type(result))
return 10, 20, 30, the comma syntax implicitly creates a TUPLE (not a list). If you assign to one variable, you get the whole tuple. If you assign to three variables (a, b, c = stats()), Python "unpacks" it. The parentheses around (10, 20, 30) are optional — it's the commas that make a tuple. This is why you can write a, b = b, a to swap two variables: the right side becomes a tuple, then the left side unpacks it.What does `return tip, total` actually return?
Add a third parameter for the number of people splitting the bill. Default it to 1 so non-split callers don't have to think about it.
def calculate_check(bill, tip_percent=15, people=1):
tip = bill * tip_percent / 100
total = bill + tip
per_person = total / people
return tip, total, per_person
tip, total, per_person = calculate_check(84.50, 18, 3)
print(f"Tip: ${tip:.2f}")
print(f"Total: ${total:.2f}")
print(f"Per person: ${per_person:.2f}")That's a real, useful function — a single line of input, three values back. Three callers can reuse it for solo dinners, dates, and group outings without changing anything.
Write a function `calculate_total(bill, tip_percent=15, tax_percent=8, people=1)` that returns (tip, tax, total, per_person). Then write a small driver that asks the user for bill, tip_percent, and people (use the defaults if they press Enter on the latter two), and prints the receipt.
Bill amount: 84.50
Tip percent (default 15): 18
Number of people (default 1): 3
Subtotal: $84.50
Tax (8%): $6.76
Tip (18%): $15.21
Total: $106.47
Per person: $35.49This code calls input(). Python here can't pause to ask you, so type the answers below before running — one value per line, in the order the program asks for them.
def calculate_total(bill, tip_percent=15, tax_percent=8, people=1):
# TODO: compute tax, tip, total, per_person
# TODO: return them as a tuple
pass
def main():
bill = float(input("Bill amount: "))
# TODO: read tip_percent with a default of 15 if user presses Enter
# TODO: read people with a default of 1 if user presses Enter
# TODO: call calculate_total and print the receipt
pass
main()
Try one of these variations:
math.ceil().