Useful Python Modules
Objectives
By the end of this chapter, you should be able to:
- Distinguish between the different ways to import
- Give examples of the data types the
collectionsmodule provides
💡 Why this matters: Reinventing things like random sampling or ordered counting wastes time. Python’s standard library already has well-tested tools for most of it.
random
import random
random.randint(1, 10) # a number between 1 and 10, inclusive
random.randrange(4) # a number between 0 and 3
random.random() # a float between 0 and 1
Importing just one function works the same way it did last lesson:
from random import choice as c
c([1, 2, 3, 4, 5]) # a random element from the list: run it a few times and watch it change
math
import math
math.e # 2.718281828459045
math.pi # 3.141592653589793
math.floor(2.2) # 2
math.ceil(2.2) # 3
math.sqrt(16) # 4.0
math.pow(2, 10) # 1024.0
collections
The collections module provides more specialized alternatives to dict, list, set, and tuple.
Counter
Tallies items automatically:
from collections import Counter
Counter([1, 1, 2, 3, 3, 4, 4, 5, 5])
# Counter({1: 2, 3: 2, 4: 2, 5: 2, 2: 1})
sentence = "this is such a nice nice nice thing that is nice!"
c = Counter(sentence.split())
# Counter({'nice': 3, 'is': 2, 'this': 1, 'such': 1, 'a': 1, 'thing': 1, 'that': 1, 'nice!': 1})
c.items() # (element, count) pairs
c.values() # just the counts
c.clear() # empty it out
defaultdict
A regular dictionary raises KeyError for a missing key. A defaultdict supplies a default instead:
from collections import defaultdict
regular_dict = dict(first=1)
regular_dict["second"] # KeyError
def default_value():
return "nothing"
d = defaultdict(default_value)
d["nice"] = "cool"
d["nice"] # "cool"
d["whoaaa"] # "nothing" (no error, just the default)
OrderedDict
from collections import OrderedDict
od = OrderedDict()
od["one"] = 1
od["two"] = 2
od["three"] = 3
for k, v in od.items():
print(k, v)
Since Python 3.7, regular dictionaries already preserve insertion order, so OrderedDict isn’t needed just to keep things in order the way it once was. It’s still useful when you specifically need order-sensitive equality comparisons (two OrderedDicts with the same pairs in a different order are not equal) or its extra .move_to_end() method.
namedtuple
A lightweight, named, immutable record: like a tuple, but with fields you can access by name:
from collections import namedtuple
Person = namedtuple("Person", "first_name last_name fav_color")
person = Person("Jordan", "Reyes", "purple")
person.fav_color # 'purple'
Try It
- Use
random.choice()to pick a random element from a list of your own. - Build a
Counterfrom a sentence of your choice and find its most common word. - Create a
namedtuplefor something in your own life (a book, a recipe, a contact) and access one of its fields by name.
Recap
randomgenerates random numbers and picks random elements;mathprovides constants and common mathematical functions.Countertallies items automatically;defaultdictsupplies a default value for missing keys instead of raisingKeyError;namedtuplegives you lightweight, named, immutable records.- Since Python 3.7, regular dictionaries preserve insertion order on their own, so
OrderedDictis now mainly useful for order-sensitive equality checks and.move_to_end().
Next lesson: testing your code with assert and unittest.