Python, properly · Chapter 9 of 42
Tuples and unpacking
By the end of this chapter you can group a few values into a tuple, take them apart again in one line, number a list with enumerate, walk two lists side by side with zip, and get several values back from one call. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Data and functions
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.
Why you need this
Bhavna runs the kitchen and the riders from a clipboard. Every line on it is a small group that belongs together: an order's id, its area and its total; a rider and the shift they work; a dish and how many were cooked. She wants the clipboard printed by a program: numbered lists, bills with a line per dish, a rota that rotates.
You could keep each group in a list, but a list says "add and remove things freely", and an order's id should never be swapped out halfway through the day. And with what you know so far, taking a group apart means one line per item, order[0], order[1], order[2], which is slow to write and easy to get wrong. Python has a better shape for a fixed group, and a way to unpack it in one go.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
rider_list.py: number the riders
Bhavna wants today's riders as a numbered list. The file starts with a list called riders: print one line per rider, numbered from 1, a dot and a space before the name. For the four riders given, exactly:
1. Santosh Kamble
2. Imran Shaikh
3. Prakash More
4. Vikas Yadav
Use enumerate, and loop over the list (the check also runs your code with other riders in it).
- Task 2
bill_lines.py: a bill from three lists
Rafiq's bill comes as three lists of the same length: dishes, quantities and prices (the price of one). Print one line per dish, then the total. For the lists given, exactly:
Dal Tadka x 1 = Rs 140
Roti x 4 = Rs 60
Jeera Rice x 2 = Rs 180
Total: Rs 380
Use zip to walk the three lists together. The check also runs your code with other dishes in the lists.
- Task 3 · interview question
Interview: rota.py, rotate without a spare variable
The file starts with three riders, one per shift: morning, afternoon and evening. Tomorrow every rider moves one shift later, and the evening rider comes round to the morning. In a single assignment, with no extra variable, change the three names, then print:
morning: Prakash
afternoon: Santosh
evening: Imran
Careful: assigning one name at a time loses a rider. The check also runs your code with other riders.
- Task 4
delivery_time.py: minutes to hours and minutes
A delivery's time arrives as a number of minutes. Read it with input(), with no prompt text, and print it in hours and minutes. For 95, exactly:
Delivered in 1 h 35 min
For 45 it is Delivered in 0 h 45 min, and for 120 it is Delivered in 2 h 0 min.
What you will learn
- Tuples: a fixed group of values that cannot change.
- Unpacking: giving every value in a group its own name in one line, including the swap a, b = b, a.
- enumerate and zip: numbering a list, and walking lists in pairs.
- Several values back: functions like divmod that return a tuple.
More in Data and functions
Build "Tuples and unpacking" yourself
DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.