Programming with Python · free preview

Modules and the Standard Library

Import functions from modules, reach for standard-library tools like math, random, collections, and datetime, and understand how pip and virtual environments fit in outside the browser.

1. Overview

Overview

2. Importing a Module

Importing a Module

3. Importing Names Directly

Importing Names Directly

4. The Standard Library Toolbox

The Standard Library Toolbox

5. Installing Packages Outside the Browser

Installing Packages Outside the Browser

6. Top Words

Top Words

7. A Week of Dates

A Week of Dates

8. Read Counter output

For Counter(["a", "b", "a"]).most_common(1), what is the exact returned list?

9. Trace a Counter and date pipeline

Follow the complete input, transformation, and output.

Example. Trace a Counter and date pipeline

Problem. Follow the complete input, transformation, and output.

Correct solution.

Step 1, import the used names.

python
from collections import Counter
from datetime import date, timedelta

Step 2, count words.

python
text = 'to be or not to be'
counts = Counter(text.split())
print(counts)
print(counts.most_common(2))

Output.```output Counter({'to': 2, 'be': 2, 'or': 1, 'not': 1}) [('to', 2), ('be', 2)]

code

Reasoning,`split()` creates six word strings; `Counter` counts each word and `most_common(2)` returns two pairs.

Step 3, build and report seven days.

```python
start = date(2026, 3, 1)
days = [start + timedelta(days=offset) for offset in range(7)]
print(days[0])
print(days[-1])
print(len(days))

Output.``output 2026-03-01 2026-03-07 7 ``

Reasoning,range(7) supplies offsets 0 through 6, so the week has seven dates.

关键提醒。

Choose standard-library modules for counting and dates; keep imports and transformations explicit.

10. Retrieval practice

Use this new input without looking back, words = 'red red blue blue blue green'. Write the standard-library call that returns the most frequent word and count in one result, and give the exact output.

Give the call and exact output.

11. Mastery check

Apply the module lesson to a local project, install requests==2.31.0, read one JSON response field, and keep dependencies reproducible. Give the terminal commands, the requirements file line, and the browser editor limitation.

Give commands, requirements line, and browser limitation.

Practice this interactively

Free account · instant grading · spaced review that schedules itself.

Start this course for free