Skip to content

ይህ ትምህርት ገና ወደ አማርኛ አልተተረጎመም፤ ስለዚህ በእንግሊዝኛ ቀርቧል። የእንግሊዝኛውን ገጽ ክፈቱ

Lists

4 min read

So far each name has referred to one value: one price, one name, one score. A shopping trip to Merkato has many prices, and a class has many exam scores. By the end of this lesson you'll keep many values under one name, pick any of them out by position, change them, and add them all up with a loop.

Every lesson in this module works fully in your browser.

Many values, one name

Predict what this prints.

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

prices = [250, 40, 1200]
print(prices[0], prices[-1])
Pick the output

Python prints

250 1200

prices[0] is the first value and prices[-1] is the last, the same positions as the characters of a string. Counting starts at 0, and negative positions count from the end.

Square brackets with values separated by commas make a . prices is one name, and it refers to one list. The list holds three items, in the order you wrote them.

Positions work exactly like the characters of a string in lesson 1.3: the first item is at index 0, and -1 is the last. len() tells you how many items there are.

prices = [250, 40, 1200]
print(len(prices))
print(prices[1])
print(prices[-2])
print(prices)

In the editor, press Escape then Tab to move on.

Add a fourth price to the list and run it again. A list can hold text too, like ["teff", "onions", "coffee"].

Three items have positions 0, 1, and 2. There is no position 3:

Part of a list

A gives you a new list with some of the items: prices[start:stop]. Like range() in lesson 3.2, the stop is not included.

prices = [250, 40, 1200, 85, 30]
print(prices[1:3])
print(prices[:2])
print(prices[3:])

In the editor, press Escape then Tab to move on.

Leave out the start and the slice begins at 0. Leave out the stop and it runs to the end. prices[1:3] has two items, positions 1 and 2.

ጥያቄ

What is [1, 2, 3][1:2]?

Lists can change

A string can't be changed in place. A list can: give one of its positions a new value.

prices = [250, 40, 1200]
prices[1] = 45
print(prices)

In the editor, press Escape then Tab to move on.

Read prices[1] = 45 as "position 1 of prices gets 45". It's still the same list; position 1 now refers to a different value. Try the same on a string and Python stops you:

That difference, lists can change and strings can't, looks small now. In lesson 4.8 it turns out to explain some of the most confusing bugs in Python.

Going through a list

for works on a list just as it works on text (lesson 3.2): each round, the loop variable refers to the next item. That makes the plans from Module 3 easy to use. Here is a running total over a list. Step through it:

Step through it

Press Next to run one line at a time and watch each name change.

prices = [250, 40, 1200]
total = 0
for price in prices:
total = total + price
print(total)
Names and their values after line 1
NameValue after line 1
prices[250, 40, 1200]new
totalnot made yet
pricenot made yet

Output so far

Step 1 of 10

Step 1 of 10. Line 1: prices = [250, 40, 1200]. prices is now [250, 40, 1200].

And here is best so far. With a list you can start from the first item, instead of a number below every possible score:

scores = [67, 82, 45]
highest = scores[0]
for score in scores:
    if score > highest:
        highest = score
print(highest)

In the editor, press Escape then Tab to move on.

These two plans are so common that Python has them built in. sum() is the running total, max() is the best so far, and min() is the lowest so far:

prices = [250, 40, 1200]
scores = [67, 82, 45]
print(sum(prices))
print(max(scores))
print(min(scores))

In the editor, press Escape then Tab to move on.

When all you need is the number, use the built-in. Write the loop when you need something extra, like printing each price on the way. Add a price to prices and run it again: nothing else has to change, however long the list gets.

መልመጃ

A Merkato shopping list

You bought four things at Merkato. Print each price with "birr" after it, then the total.

Output
120 birr
45 birr
300 birr
80 birr
Total: 545 birr

Your code

prices = [120, 45, 300, 80]
# Print each price, then the total

In the editor, press Escape then Tab to move on.

መፍትሄውን አሳይ

This is one way to solve it, not the only one. If yours prints the same thing, it works.

prices = [120, 45, 300, 80]
total = 0
for price in prices:
    print(f"{price} birr")
    total = total + price
print(f"Total: {total} birr")

ዋና ዋና ነጥቦች

  • A list is one value that holds several items in order: [250, 40, 1200].
  • Indexes work like a string's: [0] is the first item, [-1] the last, and len() counts the items.
  • A slice [start:stop] stops before stop, just like range().
  • Lists can change: prices[1] = 45 replaces one item. Strings can't.
  • for goes through a list item by item; sum(), max(), and min() are the built-in running total, best so far, and lowest so far.