ይህ ትምህርት ገና ወደ አማርኛ አልተተረጎመም፤ ስለዚህ በእንግሊዝኛ ቀርቧል። የእንግሊዝኛውን ገጽ ክፈቱ
Tuples and unpacking
Some data should never change. A coffee ceremony always has the same three rounds, in the same order. In this lesson you'll store fixed data like that in a tuple, give its parts names in one line, and finally see how the swap shortcut from lesson 1.1 works.
A list that can't change
A
is written with round brackets instead of square ones. You read it exactly like a
list: indexes, slices, len(), for, and in all work.
rounds = ("abol", "tona", "bereka")
print(rounds[1])
print(len(rounds))
for name in rounds:
print(name)In the editor, press Escape then Tab to move on.
The difference is that a tuple can never change. The ceremony has exactly these three rounds, so a tuple says what the data is: fixed. It also stops anyone, including you later, from changing it by accident:
You've already seen a tuple: lesson 2.3 promised a shorter way to write
answer == "yes" or answer == "y". Here it is:
answer = input("Ready? ")
if answer in ("yes", "y"):
print("Let's go!")In the editor, press Escape then Tab to move on.
One answer per line, given to input() in order.
in asks whether a value is one of the items. Add "Y" to the tuple so a capital Y
works too.
The comma makes the tuple
Round brackets already mean something in Python: (2 + 3) * 4 groups a calculation.
So what is (5)? Predict.
ውጤቱን ገምቱ
Decide before you look. Guessing wrong is how this sticks.
print(type((5)))
print(type((5,)))Python prints
<class 'int'> <class 'tuple'>
(5) is just 5 in grouping brackets, so it's an int. The comma is what makes a tuple: (5,) is a tuple with one item.
A tuple with one item needs a trailing comma: ("abol",). With two or more items the
commas are already there.
Unpacking: a name for each part
A tuple often holds the parts of one thing, like an item and its price. You can give each part its own name in one line. This is called :
order = ("macchiato", 35)
item, price = order
print(item)
print(price)In the editor, press Escape then Tab to move on.
The first name gets the first item, the second name the second. The number of names must match the number of items:
Unpacking also works in a for loop. When each item of a list is a tuple, give the
parts names right in the loop:
orders = [("macchiato", 35), ("sambusa", 20)]
for item, price in orders:
print(item, "costs", price)In the editor, press Escape then Tab to move on.
The swap, explained
In lesson 1.1 you swapped two values with a third name, temp, and we said the
shortcut a, b = b, a would make sense in Module 4. Now it does. Step through it:
Step through it
Press Next to run one line at a time and watch each name change.Each row is one step: the line that just ran, and every name's value after it.
| Name | Value after line 1 |
|---|---|
| a | 'tea'new |
| b | not made yet |
Output so far
Step 1 of 4
Step 1 of 4. Line 1: a = "tea". a is now 'tea'.
| Step | Line | a | b | Printed |
|---|---|---|---|---|
| 1 | 1 | 'tea' | not made yet | |
| 2 | 2 | 'tea' | 'coffee' | |
| 3 | 3 | 'coffee' | 'tea' | |
| 4 | 4 | 'coffee' | 'tea' | coffee tea |
Python works out the right side first, as always. b, a is a tuple:
("coffee", "tea"). Only then does it unpack that tuple into a and b. The tuple
already referred to both old values before either name moved, so nothing is lost.
The rest of the items
A star before a name collects all the items that are left, as a list. Predict this:
ውጤቱን ገምቱ
Decide before you look. Guessing wrong is how this sticks.
first, *rest = [90, 75, 60, 45]
print(first)
print(rest)Python prints
90 [75, 60, 45]
first gets the first item. *rest gets everything left over, and it's always a list, even when the items came from a tuple.
The star can go on any one name: *others, last = scores puts the last item in
last. Try it with the scores above.
ጥያቄ
After name, price = ("shai", 15), what is price?
መልመጃ
A café receipt
Each item of order is a tuple of an item and its price. Print a receipt: each item
in a column 12 wide on the left, its price 8 wide on the right with two decimals
(lesson 1.4), then the total.
Macchiato 35.00
Sambusa 20.00
Water 15.00
Total 70.00Your code
order = [("Macchiato", 35), ("Sambusa", 20), ("Water", 15)]
# Unpack each tuple in the loop, print a line, and keep a running totalIn the editor, press Escape then Tab to move on.
መፍትሄውን አሳይHide the solution
This is one way to solve it, not the only one. If yours prints the same thing, it works.
order = [("Macchiato", 35), ("Sambusa", 20), ("Water", 15)]
total = 0
for item, price in order:
print(f"{item:<12}{price:>8.2f}")
total = total + price
print(f"{'Total':<12}{total:>8.2f}")ዋና ዋና ነጥቦች
- A tuple is like a list that can't change:
("abol", "tona", "bereka"). Use it for fixed data. - The comma makes a tuple:
(5)is an int,(5,)is a tuple. - Unpacking gives each item a name:
item, price = order. The counts must match. a, b = b, abuilds the tuple on the right first, then unpacks it, so nothing is lost.first, *rest = itemsputs the leftover items in a list.