Skip to content

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

A tour of the standard library

4 min read

You've written a tally, a running total, and a best-so-far loop by hand. Python already ships with modules that do these jobs and many more, ready to import. By the end of this lesson you'll split a bill fairly with math, describe exam results with statistics, count with Counter, and work out how many days are left until a date.

Batteries included

The modules that come with every Python are the . You've used some already: random (lesson 3.5), json and csv (this module). Each is one import away, with nothing to install.

math has the functions a calculator has. Three friends split a 1,000 birr bill. Each share is 333.33..., and nobody pays in fractions of a santim, so round each share up to whole birr with math.ceil (ceiling), or down with math.floor:

import math

share = 1000 / 3
print(share)
print(math.ceil(share))
print(math.floor(share))
print(math.sqrt(144))

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

Rounding up means the bill is covered: 3 × 334 is 1,002. Change the 3 to 4 and run it again: 250 divides evenly, so ceil and floor agree.

random picks for you. random.seed() (lesson 3.5) makes the picks the same on every run, so you can check them; delete it for real randomness:

import random

random.seed(2026)
print(random.choice(["Bole", "Piassa", "Kazanchis", "Merkato"]))
names = ["Hana", "Abel", "Selam", "Dawit"]
random.shuffle(names)
print(names)

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

choice picks one item; shuffle reorders the list itself and returns None, like sort() in lesson 4.2.

Describing numbers

statistics describes a list of numbers. The mean is the average; the median is the middle value once they're sorted. Four exam results, an even count. Predict the median:

ውጤቱን ገምቱ

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

import statistics

print(statistics.median([72, 85, 90, 64]))
Pick the output

Python prints

78.5

Sorted, the results are 64, 72, 85, 90. With an even count there are two middle values, 72 and 85, and the median is halfway between them.

If you picked 87.5, you took the two middle values as written, 85 and 90; the median sorts first. If you picked 77.75, that's the mean, the average. With an even count, the median is halfway between the two middle values, 72 and 85:

import statistics

results = [72, 85, 90, 64]
print(statistics.mean(results))
print(statistics.median(results))
print(max(results))

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

max(), from lesson 4.1, is the best-so-far plan from lesson 3.3 in one call. The median matters when one value is far from the rest: a single 1,000 birr payment drags the mean up but barely moves the median. You'll see that in the exercise.

Counting and grouping

The tally plan from lesson 4.4 counts with a dict. collections.Counter is that plan in one line. Three coffee rounds, abol, tona, and bereka, and Selam's record of which ones she drank:

from collections import Counter

rounds = ["abol", "tona", "abol", "bereka"]
counts = {}
for name in rounds:
    counts[name] = counts.get(name, 0) + 1
print(counts)

print(Counter(rounds))
print(Counter(rounds).most_common(1))

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

The loop and Counter count the same way. most_common(1) gives the top one as a list of (item, count) tuples.

defaultdict(list) groups: every new key starts with an empty list, so you can append without checking first, the collect plan (lesson 4.6) once per key:

from collections import defaultdict

expenses = [("transport", 120), ("food", 250), ("transport", 60)]
groups = defaultdict(list)
for category, amount in expenses:
    groups[category].append(amount)
print(dict(groups))

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

ጥያቄ

You want how many times each word appears in a text. Which module has the tool?

Dates

datetime knows the calendar: month lengths, leap years, weekdays. Subtracting one date from another gives a timedelta, a length of time, whose .days is a number:

from datetime import date

start = date(2026, 10, 1)
exam = date(2027, 6, 15)
print((exam - start).days)
print(exam.strftime("%A %d %B %Y"))

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

strftime formats a date with codes: %A the weekday, %d the day, %B the month, %Y the year. date.today() gives today, so its output is different for you than for anyone else, and different tomorrow. That's why lessons use fixed dates like these:

from datetime import date

print(date.today())

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

The module and the thing you want from it can share a name, which trips up everyone once:

datetime uses the Gregorian calendar; converting to the Ethiopian calendar needs more than this module gives you.

There's much more: the standard library index lists every module. Before writing a common job yourself, check whether it's there.

መልመጃ

Telebirr amounts

Here is a week of Telebirr payments. Print the mean (rounded to 2 places with round), the median, and the most common amount with how many times it appears.

Output
Mean: 232.14
Median: 75
Most common: 50 (3 times)

Your code

import statistics
from collections import Counter

amounts = [50, 200, 50, 1000, 75, 50, 200]
# Print the mean, the median, and the most common amount

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.

import statistics
from collections import Counter

amounts = [50, 200, 50, 1000, 75, 50, 200]
print("Mean:", round(statistics.mean(amounts), 2))
print("Median:", statistics.median(amounts))
amount, times = Counter(amounts).most_common(1)[0]
print(f"Most common: {amount} ({times} times)")

The 1,000 birr payment pulls the mean to 232, while most payments were 75 or less. The median, 75, describes a typical payment better.

ዋና ዋና ነጥቦች

  • The standard library is the set of modules that come with Python: import and use, nothing to install.
  • math.ceil and math.floor round up and down; random.choice and random.shuffle pick and reorder.
  • statistics.mean is the average; statistics.median the middle value, sorted, and steadier when one value is extreme.
  • Counter is the tally plan in one line; defaultdict(list) groups without checking keys.
  • datetime.date does date arithmetic and formatting; never trust date.today() output in an example.