Python theorytheory 0/50 · 0%
Tooling · easy

16. Modules and the standard library

import, from...import, and useful stdlib modules.

`import module` brings in a module namespace; `from module import name` brings a specific name into scope directly.

python
import math
from collections import Counter

math.sqrt(16)
Counter(["a", "b", "a"]).most_common(1)

Useful standard-library modules for this track include `json` (serialize/deserialize), `hashlib` (hashing), `re` (regex), `datetime` (dates/times), `itertools` (iterator tools), and `dataclasses` (structured records).

Every module has a special `__name__` variable; the idiom `if __name__ == "__main__":` guards code that should run only when the file is executed directly, not imported.

Check your understanding

  1. 1. What does `from collections import Counter` do?

  2. 2. What is `if __name__ == "__main__":` used for?

  3. 3. Which stdlib module provides SHA-256 hashing?