Finding the right module
Before you write a function, it's worth checking whether the standard library already has one. Its modules are listed by theme in Python's documentation, at docs.python.org/3/library: text, numbers and mathematics, dates, files, data formats such as CSV and JSON, and much more. Beyond the standard library, thousands of modules written by other programmers are at the Python Package Index, pypi.org, and on your own computer you install them with a tool called pip. This site runs only the standard library.
Here are two finds that save real work.
statistics
The statistics module calculates averages and more. mean is the ordinary average, and median the middle value when the values are in order:
import statistics
lap_times = [12.5, 11.8, 13.1, 12.0, 30.2]
print(statistics.mean(lap_times))
print(statistics.median(lap_times))
15.92
12.5One lap went badly (30.2 seconds), and it pulls the mean up. The median stays a typical lap time, which is why the median is often the better summary of data with outliers. With an even number of values, the median is the mean of the two middle ones.
difflib
The difflib module compares texts. Its function get_close_matches(word, possibilities) returns the strings of the list that look most like the word, the most similar first, and an empty list when nothing is close. Python's own "Did you mean" messages work in a similar way:
from difflib import get_close_matches
commands = ["help", "start", "stop", "status", "quit"]
print(get_close_matches("hlep", commands))
print(get_close_matches("strat", commands))
print(get_close_matches("xyz", commands))
['help']
['start', 'status']
[]Reading a documentation entry
The documentation shows a function by its first line, followed by a description. The entry for this one starts with difflib.get_close_matches(word, possibilities, n=3, cutoff=0.6). The parameters with = have a default value: you can leave them out, and then the function returns at most 3 matches (n) and only fairly close ones (cutoff). Most entries work like this, so knowing how to read one lets you use functions you have never seen before.