By WebNest Studio Engineering Team
Python Tutorial
A Tour of the Python Standard Library
One of Python's biggest practical advantages is its extensive standard library — the collection of modules that ship with every Python installation, requiring no extra "pip install" step. This is often summarized as Python coming with "batteries included." Knowing what already exists in the standard library saves you from reinventing common functionality or reaching for a third-party dependency unnecessarily.
This tour covers a handful of the most frequently used standard library modules: os, sys, datetime, collections, and itertools. Each deserves a full reference on its own, but a working familiarity with what each one is for will cover the majority of everyday scripting needs.
Why it matters
Python's standard library covers dates, files, JSON, HTTP, random numbers, counting and much more, with nothing to install. Checking it before writing your own code or adding a dependency saves time and avoids bugs. Knowing roughly what it contains is one of the things that separates experienced Python developers from beginners.
os and sys — Operating System and Interpreter Access
The os module provides a portable way to interact with the operating system: reading and setting environment variables (os.environ), joining file paths correctly across platforms (os.path.join), listing directory contents (os.listdir), and creating or removing directories. The sys module, by contrast, exposes details about the Python interpreter itself and the running program — sys.argv holds command-line arguments, sys.path is the module search path described earlier, and sys.exit() terminates the program with a given exit code.
datetime — Dates, Times, and Durations
The datetime module provides classes for representing dates (date), times (time), and combined date-times (datetime), along with a timedelta class for representing durations and doing date arithmetic. datetime.now() gets the current local date and time, and calling .strftime(format_string) on any datetime object formats it into a human-readable string.
collections — Specialized Container Types
Beyond the built-in list, dict, set, and tuple, the collections module provides specialized alternatives: Counter tallies occurrences of items in an iterable, defaultdict supplies an automatic default value for missing keys so you don't need to check for existence first, namedtuple creates lightweight, immutable objects with named fields, and deque is a double-ended queue optimized for fast appends and pops from both ends.
itertools — Efficient Iteration Building Blocks
The itertools module provides fast, memory-efficient functions for working with iterators. itertools.chain joins multiple iterables into one logical sequence, itertools.permutations and itertools.combinations generate arrangements of elements, and itertools.count produces an infinite counting sequence. These functions return iterators (computed lazily, one item at a time) rather than fully built lists, which matters a great deal for performance on large datasets.
Examples
os, datetime, and sys together
import os
import sys
from datetime import datetime
print(os.path.join("data", "reports", "2026.csv"))
print(len(sys.argv) >= 1) # sys.argv always has at least the script name
now = datetime(2026, 9, 21, 14, 30)
print(now.strftime("%Y-%m-%d %H:%M"))
data/reports/2026.csv
True
2026-09-21 14:30
collections.Counter and itertools.combinations
from collections import Counter
from itertools import combinations
words = ["apple", "banana", "apple", "cherry", "banana", "apple"]
counts = Counter(words)
print(counts.most_common(2))
items = ["a", "b", "c"]
pairs = list(combinations(items, 2))
print(pairs)
[('apple', 3), ('banana', 2)]
[('a', 'b'), ('a', 'c'), ('b', 'c')]
Common Mistakes
- Manually joining file paths with string concatenation and "/" instead of os.path.join (or pathlib), which breaks on Windows-style paths.
- Using a plain dict and checking "if key in dict" before every increment, instead of using collections.Counter or defaultdict to simplify the logic.
- Forgetting that itertools functions return lazy iterators, then trying to check their length with len() or index into them directly — you usually need to wrap them in list() first.
- Parsing dates with manual string slicing instead of datetime.strptime, leading to fragile code that breaks on unexpected formats.
Exercise
Use datetime to print the date 30 days after 31 January 2026. Then use itertools to print every pair that can be chosen from three letters.
Expected output: 2026-03-02 then [('a', 'b'), ('a', 'c'), ('b', 'c')]
from datetime import date, timedelta
from itertools import combinations
start = date(2026, 1, 31)
# TODO: print the date 30 days after start
letters = ["a", "b", "c"]
# TODO: print a list of all pairs of letters
Show hints
- Adding a
timedelta(days=30)to a date gives a new date. combinations(letters, 2)yields the pairs; wrap it inlist()to print them.
Show solution
from datetime import date, timedelta
from itertools import combinations
start = date(2026, 1, 31)
print(start + timedelta(days=30))
letters = ["a", "b", "c"]
print(list(combinations(letters, 2)))
Quick quiz
1. What does the phrase "batteries included" say about Python?
- A. It ships with a large standard library ready to use
- B. It needs no electricity
- C. It includes a database server
- D. It cannot use external packages
Show answer
A. Many common tasks need no third-party package.
2. Which module would you use to work with dates and times?
- A. time_utils
- B. datetime
- C. calendar_api
- D. os
Show answer
B. It provides date, time, datetime and timedelta.
3. How do you use a standard library module?
- A. Install it with pip first
- B. Download it from GitHub
- C. Import it; it is already installed with Python
- D. Enable it in the settings
Show answer
C. Only third-party packages need to be installed.
Interview questions
Which standard library modules do you use most often?
Show answer
A good answer names several with what they are for: os, sys and pathlib for the system and file paths; datetime for dates; json and csv for data formats; collections and itertools for data handling; re for regular expressions; logging for logs; and unittest for tests.
What is the itertools module for?
Show answer
It provides fast, memory-efficient building blocks for working with iterables. chain joins iterables, islice takes part of one, groupby groups consecutive items, and product, permutations and combinations generate arrangements. They all return iterators, so values are produced only as they are consumed.
Key Points to Remember
- The standard library ships with every Python install — "batteries included" — no pip install required.
- os and sys handle operating system interaction and interpreter/runtime details, respectively.
- datetime models dates, times, and durations, with strftime/strptime for formatting and parsing.
- collections and itertools provide specialized, efficient alternatives to hand-rolled data structures and loops.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering