Lesson 42 of 60
โฎPrev Nextโฏ

PY-101 ยท Modules and packages

The standard library tour

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

42of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    The standard library is the large set of modules that comes with Python, so you can use them without installing anything. Everyday favourites include datetime for dates, collections for Counter and defaultdict, pathlib for file paths, random, json, csv and itertools. Check it before writing a tool yourself.

    • Count with collections.Counter
    • Work with dates and timedelta
    • Know where to look before reinventing

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    The standard library tour, running
    1. from collections import Counter
    2. from datetime import date, timedelta
    3. from pathlib import Path
    4. votes = ["tea", "coffee", "tea", "juice", "tea", "coffee"]
    5. print(Counter(votes).most_common(2))
    6. due = date(2026, 12, 1) + timedelta(days=45)
    7. print("Due:", due)
    8. report = Path("reports") / "2026" / "dec.csv"
    9. print(report.name, report.suffix, report.parent.name)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemYou write 15 lines to count words, then discover Counter does it in one. The standard library is full of those moments; learning what exists saves hours.
    2. The Python ideafrom collections import Counter, defaultdictThe standard library is the large set of modules that comes with Python, so you can use them without installing anything.
    3. In real lifeBilling systems compute due dates with timedelta, and analytics scripts count events with Counter before reaching for bigger libraries.

    Start with a question

    You write 15 lines to count words, then discover Counter does it in one. The standard library is full of those moments; learning what exists saves hours.

    Professional Python leans heavily on the standard library. Knowing it makes your code shorter and more reliable.

    Explain it like I'm 10

    Python comes with a huge free toolbox already in the house. Before you build a tool, check whether it is already in the box.

    The technical explanation

    Counter(words).most_common(2) returns the two most frequent items with their counts.

    date + timedelta(days=30) does date maths, including month ends and leap years.

    Path("reports") / "2026" / "dec.csv" builds file paths that work on every operating system.

    Syntax

    from collections import Counter, defaultdict
    from datetime import date, timedelta
    from pathlib import Path

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Billing systems compute due dates with timedelta, and analytics scripts count events with Counter before reaching for bigger libraries.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. Do standard library modules need pip install?
    2. 2. Which module has Counter?
    3. 3. date(2026, 1, 31) + timedelta(days=1)?
    4. 4. Path("a") / "b.txt" gives?
    5. 5. Best first step before writing a utility?

    Questions people ask

    What does "batteries included" mean in Python?

    That Python ships with a large standard library for common jobs (files, dates, JSON, maths, networking), so you can do a lot without installing anything.

    What is the difference between the standard library and PyPI?

    The standard library comes with Python. PyPI is the public index of extra packages, such as pandas or requests, that you install with pip.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner What does "batteries included" mean in Python?

    That Python ships with a large standard library for common jobs (files, dates, JSON, maths, networking), so you can do a lot without installing anything.

    beginner What is the difference between the standard library and PyPI?

    The standard library comes with Python. PyPI is the public index of extra packages, such as pandas or requests, that you install with pip.

    intermediate Do standard library modules need pip install?

    No, they ship with Python. They come with Python.

    intermediate Which module has Counter?

    collections. collections.Counter.

    Quick revision

    Remember

    • Count with collections.Counter
    • Work with dates and timedelta
    • Know where to look before reinventing
    • Python's motto is "batteries included". Search docs.python.org/3/library before pip installing something.

    Syntax

    from collections import Counter, defaultdict
    from datetime import date, timedelta
    from pathlib import Path

    Official documentation