17
Lesson 35 of 44

Standard library

Python ships with hundreds of ready-to-use modules. In this lesson you will meet the most useful ones: datetime to work with dates, collections with Counter and defaultdict, statistics to compute averages and medians, and itertools and string for common tasks. You will also learn to read the documentation to find what you need, with practical exercises.

  • 6 theory articles
  • 11 practice exercises
Start lesson

Fundamentals concepts 6

collections: Counter and defaultdict

The collections module offers specialized data structures. The two most useful ones solve perfectly the counting and grouping…

Theory
Read more

statistics: mean, median and deviation

The statistics module calculates the most common statistical measures of a list of numbers. With Python's built-in functions (…

Theory
Read more

itertools and string

Two small modules that solve very specific tasks: itertools generates combinations of elements and string offers ready-made…

Theory
Read more

How to find and read the documentation

Nobody knows the standard library by heart. The most useful skill of a programmer is not remembering every function, but knowing…

Theory
Read more

bisect and heapq: sorted lists and priority queues

Two small modules of the standard library solve problems that you would otherwise program by hand: keeping a list sorted and…

Theory
Read more

functools, operator and textwrap

Three more standard library modules that save code and make programs clearer.

Theory
Read more

Practice exercises 11

Due date

Create a Python program that calculates the due date of an invoice by adding some days to the issue date.

Exercise Medium
Solve it

Most common words

Create a Python program that shows the three words that are repeated the most in a text using Counter .

Exercise Medium
Solve it

Cities by country

Create a Python program that groups cities by country using defaultdict .

Exercise Medium
Solve it

Statistics summary

Create a Python program that calculates the mean, the median and the standard deviation of a series of numbers with the…

Exercise Medium
Solve it

Password generator

Create a Python program that generates a random password with the random and string modules.

Exercise Medium
Solve it

Anagrams

Create a Python program that shows all the distinct anagrams of a word using itertools.permutations .

Exercise Medium
Solve it

Grade table with bisect

The bisect module finds positions in sorted lists using binary search. It is ideal for turning a number into a category according…

Exercise Easy
Solve it

The k largest and smallest

The heapq module implements heaps (priority queues). With nlargest and nsmallest you get the extremes of a list without sorting…

Exercise Medium
Solve it

Memoization with lru_cache

The @lru_cache decorator from the functools module stores the results of a function so they are not recalculated. It also lets…

Exercise Medium
Solve it

Sorting with itemgetter

operator.itemgetter creates a function that extracts one or several items from each record. It is the clean way to sort by…

Exercise Medium
Solve it

Fitting text to a width

The textwrap module formats paragraphs: it splits a text into lines of a maximum width and can shorten it by adding an ellipsis.

Exercise Medium
Solve it