Previous | Tutorial index | Next

Tutorial 5: The Python Standard Library – Common Modules and Their Uses

Learning Objectives

Explain the functionalities of some standard and widely used modules, and use them comfortably in programming.

1. Introduction: The "Batteries Included" Philosophy

Python is famous for its philosophy of being "batteries included." This means that when you install Python, you get a vast, powerful, and comprehensive Standard Library – a collection of over 200 modules that handle everything from file compression to web services, from mathematical computations to system administration.

Why is this important? Because you don't need to reinvent the wheel for common programming tasks. Need to parse a JSON response from an API? There's a module for that. Need to generate a random password? There's a module for that. Need to work with dates and times across time zones? There's a module for that too.

In this tutorial, we will explore 10 of the most widely used standard library modules. Mastering these will dramatically increase your productivity and allow you to write professional-grade Python code without relying on third-party dependencies for basic tasks.

2. Module 1: math – Mathematical Operations

The math module provides access to mathematical functions defined by the C standard. It is the go-to module for pure mathematical computations.

Key Features:

Category Functions/Constants Description
Constants pi, e, tau, inf, nan Mathematical constants (π, Euler's number, etc.).
Trigonometry sin(), cos(), tan(), asin(), acos(), atan() Standard trigonometric functions (angles in radians).
Conversion degrees(), radians() Convert between degrees and radians.
Logarithms log(), log10(), log2() Natural log, base-10 log, base-2 log.
Exponents/Roots exp(), sqrt(), pow() Exponential, square root, and power functions.
Rounding ceil(), floor(), trunc() Round up, round down, and truncate decimals.
Combinatorics factorial(), comb(), perm() Factorial, combinations, and permutations.

Practical Examples:

import math # Constants print(f"π = {math.pi}") # 3.141592653589793 print(f"e = {math.e}") # 2.718281828459045 # Trigonometry (angles in radians) angle_deg = 45 angle_rad = math.radians(angle_deg) print(f"sin(45°) = {math.sin(angle_rad):.4f}") # 0.7071 # Square root and powers print(f"sqrt(144) = {math.sqrt(144)}") # 12.0 print(f"2^10 = {math.pow(2, 10)}") # 1024.0 # Rounding print(f"ceil(4.2) = {math.ceil(4.2)}") # 5 print(f"floor(4.9) = {math.floor(4.9)}") # 4 # Factorials and combinations print(f"5! = {math.factorial(5)}") # 120 print(f"C(10, 3) = {math.comb(10, 3)}") # 120

Important Note: The math module works with floats. If you need arbitrary precision or complex numbers, look at the decimal and cmath modules.

3. Module 2: random – Generating Pseudo-Random Numbers

The random module is used for generating pseudo-random numbers, selecting random elements, and shuffling sequences. It is essential for simulations, games, sampling, and security (though not for cryptographic purposes – use secrets for that).

Key Features:

Function Description
random() Generates a random float between 0.0 and 1.0.
randint(a, b) Returns a random integer between a and b (inclusive).
randrange(start, stop, step) Returns a random element from the given range.
choice(sequence) Returns a random element from a non-empty sequence.
choices(sequence, k=n) Returns a list of n random elements with replacement.
sample(sequence, k=n) Returns a list of n unique random elements (without replacement).
shuffle(sequence) Shuffles a mutable sequence in place.
uniform(a, b) Returns a random float between a and b.
seed(a) Initializes the random number generator with a seed for reproducibility.

Practical Examples:

import random # Reproducibility (useful for debugging) random.seed(42) print(random.random()) # Always 0.6394267984578837 with seed 42 # Integers print(random.randint(1, 6)) # Simulate a die roll print(random.randrange(0, 100, 10)) # Random multiple of 10 # Sequences colors = ['red', 'green', 'blue', 'yellow'] print(random.choice(colors)) # Random color print(random.sample(colors, 2)) # 2 unique colors print(random.choices(colors, k=5)) # 5 random colors (with repeats) # Shuffling cards = list(range(1, 53)) random.shuffle(cards) print(cards[:5]) # First 5 shuffled cards

Key Point: The randomness is pseudo-random – it's deterministic based on the seed. For true randomness (e.g., for security tokens), use the secrets module instead.

4. Module 3: datetime – Dates and Times

The datetime module provides classes for manipulating dates and times in both simple and complex ways. It handles leap years, time zones, and time arithmetic.

Key Classes:

Class Purpose
date Represents a date (year, month, day).
time Represents a time (hour, minute, second, microsecond).
datetime Represents a combination of date and time.
timedelta Represents a duration or difference between two dates/times.

Key Functions:

Function Description
datetime.now() Returns the current local date and time.
datetime.today() Returns the current local datetime (same as now).
datetime.strptime(date_string, format) Parses a string into a datetime object.
datetime.strftime(format) Formats a datetime object into a string.

Practical Examples:

from datetime import datetime, date, time, timedelta # Current date and time now = datetime.now() print(f"Now: {now}") # 2026-08-11 14:30:25.123456 print(f"Date: {now.date()}") # 2026-08-11 print(f"Time: {now.time()}") # 14:30:25.123456 # Creating specific dates birthday = date(1990, 5, 15) print(birthday) # Date arithmetic (timedelta) one_week = timedelta(days=7) next_week = now + one_week print(f"Next week: {next_week}") # Formatting (strftime) - converting datetime to string formatted = now.strftime("%A, %B %d, %Y at %I:%M %p") print(formatted) # Tuesday, August 11, 2026 at 02:30 PM # Parsing (strptime) - converting string to datetime date_str = "2025-12-25 18:30:00" parsed = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S") print(parsed) # 2025-12-25 18:30:00 # Calculating age birth_date = date(1990, 5, 15) today = date.today() age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day)) print(f"Age: {age}")

Format Codes Cheat Sheet:

5. Module 4: os – Operating System Interface

The os module provides a portable way to use operating system-dependent functionality. It allows you to interact with the file system, environment variables, and processes.

Key Features:

Function Description
os.getcwd() Returns the current working directory.
os.chdir(path) Changes the current working directory.
os.listdir(path) Returns a list of all files/directories in a path.
os.mkdir(path) Creates a new directory.
os.makedirs(path) Creates nested directories (like mkdir -p).
os.remove(path) Deletes a file.
os.rmdir(path) Removes an empty directory.
os.rename(old, new) Renames a file or directory.
os.path.join(a, b) Joins path components (works cross-platform).
os.path.exists(path) Checks if a path exists.
os.path.isfile(path) Checks if the path is a file.
os.path.isdir(path) Checks if the path is a directory.
os.environ A dictionary of environment variables.
os.system(command) Runs a shell command (use with caution).

Practical Examples:

import os # Current directory and listing files cwd = os.getcwd() print(f"Current directory: {cwd}") print(f"Files in current dir: {os.listdir('.')}") # Creating directories os.makedirs("my_project/data", exist_ok=True) # exist_ok prevents error if exists # Path manipulation (cross-platform safe) path = os.path.join("my_project", "data", "file.txt") print(f"Path: {path}") # On Windows: my_project\data\file.txt; on Linux: my_project/data/file.txt # Checking if a file or directory exists if os.path.exists("my_project"): print("my_project exists") # Environment variables home = os.environ.get("HOME") # or "USERPROFILE" on Windows print(f"Home directory: {home}") # Executing a shell command (be careful!) os.system("echo 'Hello from Python'")

Best Practice: For modern, object-oriented path manipulation, consider using pathlib (covered later in this tutorial) instead of os.path. However, os is still essential for environment variables, process management, and OS-level functions.

6. Module 5: sys – Python Interpreter Interaction

The sys module provides access to variables and functions that interact with the Python interpreter itself. It's crucial for command-line scripting, debugging, and runtime configuration.

Key Features:

Attribute/Function Description
sys.argv List of command-line arguments passed to the script.
sys.exit() Exits the program (can pass an exit code).
sys.path List of directories Python searches for modules.
sys.version The Python version string.
sys.platform The operating system identifier (e.g., 'win32', 'linux').
sys.stdin, sys.stdout, sys.stderr Standard input, output, and error streams.
sys.getsizeof() Returns the size of an object in bytes.
sys.modules Dictionary of all loaded modules.

Practical Examples:

import sys # Getting Python version print(f"Python version: {sys.version}") print(f"Platform: {sys.platform}") # Command-line arguments (save this as script.py and run: python script.py arg1 arg2) if len(sys.argv) > 1: print(f"Arguments received: {sys.argv[1:]}") else: print("No arguments provided.") # Module search path print(f"First search path: {sys.path[0]}") # Usually the current directory # Exiting with an error code def validate_input(value): if value < 0: print("Error: Value cannot be negative.") sys.exit(1) # Exit with error code 1 # Memory usage my_list = [1, 2, 3, 4, 5] print(f"Size of list: {sys.getsizeof(my_list)} bytes")

Use Case: sys.argv is the foundation of building command-line tools in Python. Combine it with argparse for more sophisticated argument parsing.

7. Module 6: json – Working with JSON Data

JSON (JavaScript Object Notation) is the most ubiquitous data interchange format on the web. The json module allows you to convert Python objects to JSON strings and vice versa.

Key Functions:

Function Description
json.dumps(obj) Serializes a Python object to a JSON string.
json.dump(obj, file) Writes a Python object as JSON to a file.
json.loads(string) Deserializes a JSON string into a Python object.
json.load(file) Reads JSON data from a file and deserializes it.

Type Mapping (Python ↔ JSON):

Python JSON
dict object
list, tuple array
str string
int, float number
True, False true, false
None null

Practical Examples:

import json # Python dictionary to JSON string data = { "name": "Alice", "age": 30, "is_student": False, "courses": ["Math", "Physics"], "address": { "city": "New York", "zip": 10001 } } json_string = json.dumps(data, indent=4) # indent for pretty printing print(json_string) # JSON string to Python dictionary json_input = '{"name": "Bob", "age": 25, "active": true, "score": null}' parsed_data = json.loads(json_input) print(parsed_data["name"]) # Bob print(parsed_data.get("score")) # None # Reading and writing JSON files with open("data.json", "w") as f: json.dump(data, f, indent=4) with open("data.json", "r") as f: loaded_data = json.load(f) print(loaded_data["name"]) # Alice

Common Pitfall: JSON keys must be strings. Python dictionaries with non-string keys will raise a TypeError when serialized. Use the default parameter to handle custom objects.

8. Module 7: csv – Reading and Writing CSV Files

CSV (Comma-Separated Values) is a simple file format used for spreadsheets and data exchange. The csv module provides functionality to read and write CSV files easily.

Key Features:

Function Description
csv.reader(file) Returns a reader object that iterates over rows.
csv.writer(file) Returns a writer object to write rows.
csv.DictReader(file) Reads rows as dictionaries (header row as keys).
csv.DictWriter(file, fieldnames) Writes rows from dictionaries.

Practical Examples:

import csv # Writing to a CSV file data = [ ["Name", "Age", "City"], ["Alice", 30, "New York"], ["Bob", 25, "Los Angeles"], ["Charlie", 35, "Chicago"] ] with open("people.csv", "w", newline="") as f: writer = csv.writer(f) writer.writerows(data) # Write all rows at once # Reading from a CSV file with open("people.csv", "r") as f: reader = csv.reader(f) for row in reader: print(row) # Each row is a list # Using DictReader (assumes first row is header) with open("people.csv", "r") as f: reader = csv.DictReader(f) for row in reader: print(f"{row['Name']} is {row['Age']} years old and lives in {row['City']}.") # Writing with DictWriter data_dicts = [ {"Name": "David", "Age": "40", "City": "Boston"}, {"Name": "Eve", "Age": "28", "City": "Seattle"} ] with open("people_dict.csv", "w", newline="") as f: fieldnames = ["Name", "Age", "City"] writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() writer.writerows(data_dicts)

Important: Always use newline="" when opening CSV files to prevent issues with line endings on different operating systems.

9. Module 8: re – Regular Expressions (Pattern Matching)

The re module provides regular expression matching operations. Regular expressions are a powerful language for pattern matching in text. They are essential for searching, validating, and manipulating strings.

Key Functions:

Function Description
re.search(pattern, string) Scans for a match anywhere in the string.
re.match(pattern, string) Matches only at the beginning of the string.
re.findall(pattern, string) Returns a list of all non-overlapping matches.
re.finditer(pattern, string) Returns an iterator of match objects.
re.sub(pattern, repl, string) Replaces matches with a replacement string.
re.split(pattern, string) Splits the string by the pattern.
re.compile(pattern) Compiles a pattern for better performance (reuse).

Common Regex Patterns (Metacharacters):

Pattern Description
. Any character (except newline).
^ Start of string.
$ End of string.
* Zero or more repetitions.
+ One or more repetitions.
? Zero or one repetition.
\d Any digit (0-9).
\w Any alphanumeric or underscore.
\s Any whitespace.
[a-z] Character class (any lowercase letter).
(abc) Capturing group.

Practical Examples:

import re text = "My email is alice@example.com and my phone is 123-456-7890." # Searching for a pattern email_pattern = r"\w+@\w+\.\w+" # Simple email pattern match = re.search(email_pattern, text) if match: print(f"Found email: {match.group()}") # Find all matches phone_pattern = r"\d{3}-\d{3}-\d{4}" phones = re.findall(phone_pattern, text) print(f"Phone numbers: {phones}") # ['123-456-7890'] # Replacing text new_text = re.sub(phone_pattern, "[REDACTED]", text) print(new_text) # My email is alice@example.com and my phone is [REDACTED]. # Compiling for performance pattern = re.compile(r"\b[A-Z][a-z]+\b") # Capitalized words cap_words = pattern.findall("Hello World! This is Python.") print(cap_words) # ['Hello', 'World', 'This', 'Python'] # Splitting strings parts = re.split(r"\s+", "Split this text by spaces") print(parts) # ['Split', 'this', 'text', 'by', 'spaces'] # Validation def is_valid_email(email): pattern = r"^[\w\.-]+@[\w\.-]+\.\w+$" # More robust pattern return re.match(pattern, email) is not None print(is_valid_email("test@example.com")) # True print(is_valid_email("invalid-email")) # False

Pro Tip: Regular expressions can get complex. Use online tools like regex101.com to test and debug your patterns. Remember that re is powerful but can be slow for massive text processing; for simple string operations, str methods are often sufficient.

10. Module 9: collections – Specialized Container Data Types

The collections module provides alternatives to Python's built-in containers (list, dict, set, tuple) with specialized functionality.

Key Data Types:

Type Description
Counter A dictionary subclass for counting hashable objects.
defaultdict A dictionary that provides a default value for missing keys.
deque A double-ended queue for fast appends and pops on both ends.
namedtuple A tuple with named fields (lightweight data classes).
OrderedDict A dictionary that remembers insertion order (Python 3.7+ dicts are ordered by default, but this is still useful for some cases).

Practical Examples:

from collections import Counter, defaultdict, deque, namedtuple # 1. Counter - Counting elements text = "mississippi" count = Counter(text) print(count) # Counter({'i': 4, 's': 4, 'p': 2, 'm': 1}) print(count.most_common(2)) # [('i', 4), ('s', 4)] # 2. defaultdict - Auto-handle missing keys my_dict = defaultdict(list) # Default value is an empty list my_dict['fruits'].append('apple') my_dict['fruits'].append('banana') my_dict['vegetables'].append('carrot') print(dict(my_dict)) # {'fruits': ['apple', 'banana'], 'vegetables': ['carrot']} # 3. deque - Fast queues and stacks queue = deque(['a', 'b', 'c']) queue.append('d') queue.appendleft('z') print(queue) # deque(['z', 'a', 'b', 'c', 'd']) print(queue.pop()) # 'd' print(queue.popleft()) # 'z' # 4. namedtuple - Lightweight immutable data objects Point = namedtuple('Point', ['x', 'y']) p1 = Point(10, 20) print(p1.x, p1.y) # 10 20 # Access by index as well (like a tuple) print(p1[0]) # 10

11. Module 10: pathlib – Modern Path Manipulation

The pathlib module offers an object-oriented approach to file system paths. It is designed to be more intuitive and less error-prone than os.path. Introduced in Python 3.4, it is now the recommended way to handle paths.

Key Features:

Class/Attribute Description
Path The main class for representing a path.
Path.cwd() Returns the current working directory as a Path object.
Path.home() Returns the user's home directory.
.exists() Checks if the path exists.
.is_file() / .is_dir() Checks if the path is a file or directory.
.iterdir() Iterates over the contents of a directory.
.mkdir() Creates a directory (with parents=True for nested).
.read_text() / .write_text() Read/write text files directly.
.stem The filename without extension.
.suffix The file extension (including the dot).
.parent Returns the parent directory.
/ Operator for joining paths (e.g., Path('/home') / 'user' / 'file.txt').

Practical Examples:

from pathlib import Path # Creating paths (cross-platform safe) home = Path.home() project_dir = home / "my_project" / "data" file_path = project_dir / "config.json" print(file_path) # /home/user/my_project/data/config.json (Linux) or C:\Users\User\my_project\data\config.json (Windows) # Checking existence and creating directories if not project_dir.exists(): project_dir.mkdir(parents=True) # Creates all intermediate directories # Reading and writing files file_path.write_text('{"key": "value"}') content = file_path.read_text() print(content) # {"key": "value"} # Working with directory contents for item in Path.home().iterdir(): if item.is_file(): print(f"File: {item.name}") elif item.is_dir(): print(f"Directory: {item.name}") # Accessing path components p = Path("/home/user/docs/report.pdf") print(p.name) # report.pdf print(p.stem) # report print(p.suffix) # .pdf print(p.parent) # /home/user/docs print(p.parent.parent) # /home/user # Glob pattern matching (advanced) for py_file in Path.home().glob("**/*.py"): # Recursive search for .py files print(py_file)

Why Use pathlib? It's more readable, more concise, and works uniformly across Windows, Linux, and macOS without needing os.path.join or conditionals. It is now the preferred standard library module for path operations.

12. Quiz: Check Your Understanding

1. Which module would you use to generate a random integer between 1 and 100?

Answer(B) `random` (specifically `random.randint(1, 100)`)

2. You have a string "2025-12-25 10:30:00". Which function correctly parses this into a datetime object?

Answer(B) `datetime.strptime("2025-12-25 10:30:00", "%Y-%m-%d %H:%M:%S")`

3. Which module would you use to get the command-line arguments passed to your Python script?

Answer(C) `sys` (specifically `sys.argv`)

4. What does the json.dumps() function do?

Answer(B) Converts a Python dictionary to a JSON string.

5. Which collections data type is specifically designed for fast appends and pops at both ends of a sequence?

Answer(C) `deque` (double-ended queue)

6. Which module provides an object-oriented and cross-platform way to handle file paths?

Answer(C) `pathlib`

7. True or False: The re module is used for mathematical regular expressions (like solving equations).

AnswerFalse. It's for regular expressions, not mathematical expressions.

8. You need to count the frequency of each word in a large text. Which collections class is most suitable?

Answer(C) `Counter` (as it's designed for counting hashable objects)

5.13 Exercises

Exercise 1: Random Password Generator

Goal: Generate a strong random password of a given length.

Instructions:

  1. Import random and string.
  2. Define a function generate_password(length=12) that:
  3. Generate and print three passwords of length 16.
Sample Solution
import random import string def generate_password(length=12): chars = string.ascii_letters + string.digits + string.punctuation return ''.join(random.choices(chars, k=length)) for i in range(3): print(f"Password {i+1}: {generate_password(16)}")

Exercise 2: File System Explorer

Goal: Write a script that lists all files and directories in a given path using both os and pathlib (for comparison).

Instructions:

  1. Import os and pathlib.
  2. Ask the user to input a directory path (use input()).
  3. Using os.listdir() and os.path.isdir()/os.path.isfile(), print all items, categorizing them as [DIR] or [FILE].
  4. Using pathlib.Path(path).iterdir() and checking .is_dir() and .is_file(), do the same thing.
  5. Print the total number of items found.
Sample Solution
import os from pathlib import Path path = input("Enter directory path: ") # Using os print("Using os:") os_items = os.listdir(path) for item in os_items: full = os.path.join(path, item) if os.path.isdir(full): print(f"[DIR] {item}") else: print(f"[FILE] {item}") print(f"Total: {len(os_items)}") # Using pathlib print("\nUsing pathlib:") p = Path(path) items = list(p.iterdir()) for item in items: if item.is_dir(): print(f"[DIR] {item.name}") else: print(f"[FILE] {item.name}") print(f"Total: {len(items)}")

Exercise 3: JSON Configuration Reader/Writer

Goal: Create a simple configuration manager that loads settings from a JSON file and allows updating them.

Instructions:

  1. Create a JSON file config.json with the following content:
    { "username": "admin", "theme": "dark", "notifications": true, "language": "en" }
  2. Write a Python script that:
  3. Reload the file and print the updated settings to confirm.
Sample Solution
import json # Load with open("config.json", "r") as f: config = json.load(f) print("Current config:", config) # Update new_theme = input("Enter new theme (light/dark): ") config["theme"] = new_theme # Save with open("config.json", "w") as f: json.dump(config, f, indent=4) # Reload and verify with open("config.json", "r") as f: updated = json.load(f) print("Updated config:", updated)

Exercise 4: Log File Analyzer with Counter

Goal: Analyze a server log file and count the frequency of different status codes.

Instructions:

  1. Create a text file server.log with the following lines (simplified log format):
    192.168.1.1 - GET /index 200 10.0.0.1 - POST /login 401 192.168.1.1 - GET /about 200 10.0.0.2 - GET /contact 404 192.168.1.1 - POST /data 500 10.0.0.1 - GET /home 200 10.0.0.2 - GET /index 200
  2. Write a Python script that:
Sample Solution
from collections import Counter status_codes = [] with open("server.log", "r") as f: for line in f: parts = line.split() if parts: status = parts[-1] status_codes.append(status) counter = Counter(status_codes) for code, count in counter.most_common(): print(f"{code}: {count}")

Exercise 5: Regex Email Validator

Goal: Build a function that validates email addresses using regular expressions.

Instructions:

  1. Import the re module.
  2. Define a function validate_email(email) that:
  3. Test your function with at least 5 valid and 5 invalid email addresses.
  4. Bonus: Use re.findall() to extract all valid emails from a block of text.
Sample Solution
import re def validate_email(email): pattern = r"^[\w\.-]+@[\w\.-]+\.\w{2,6}$" return re.match(pattern, email) is not None emails = [ "user@example.com", "first.last@domain.co.uk", "invalid-email", "missing@dotcom", "user@.com", "user@domain.c" ] for e in emails: print(f"{e}: {validate_email(e)}") # Bonus extraction text = "Contact us at support@company.com or sales@company.net for more info." valid_emails = re.findall(r"[\w\.-]+@[\w\.-]+\.\w{2,6}", text) print(valid_emails)

5.14 Common Pitfalls and Troubleshooting

Problem Likely Cause Solution
AttributeError: module 'random' has no attribute 'randint' You accidentally named your own script random.py in the current directory, shadowing the built-in module. Rename your script to something else (e.g., my_random.py).
FileNotFoundError when reading a CSV or JSON file. The file path is incorrect or the file doesn't exist. Use absolute paths, or ensure the current working directory is correct (os.getcwd()). Use os.path.exists() to check first.
Dates are parsed incorrectly with strptime. The format string doesn't match the actual string. Double-check the format codes. Use %Y for 4-digit years, %y for 2-digit. Check for spaces, colons, and separators.
JSON serialization fails with TypeError: Object of type ... is not JSON serializable. You tried to serialize a custom object (like a datetime). Use the default parameter: json.dumps(obj, default=str) to convert unknown types to strings.
re pattern doesn't match what you expect. Special characters (like ., *, ?) are not escaped. Use raw strings (e.g., r"pattern") to avoid backslash issues. Escape special characters with \ (e.g., \. for a literal dot).
os.system() doesn't work or gives strange output. It's platform-dependent; the command might not be available on your OS. Use the subprocess module for more control and cross-platform compatibility.
pathlib operations raise PermissionError. You don't have read/write permissions for the file or directory. Check your file system permissions. Run your script with appropriate privileges.

5.15 Homework Questions

Answer the following questions in complete sentences. Each response should be 3–5 sentences unless otherwise specified.

Short Answer Questions

1. Why is the Python standard library often described as "batteries included"? Give two examples of modules that demonstrate this philosophy.

Sample AnswerThis phrase means that Python comes with a wide range of built‑in modules that handle common tasks, so you don't need to install external libraries for basic functionality. For example, the `json` module lets you work with JSON data without any extra downloads, and the `csv` module provides tools to read and write spreadsheet‑like files. This saves time and reduces dependencies, making Python very productive out of the box.

2. What is the difference between random.random() and random.randint(a, b)?

Sample Answer`random.random()` returns a random floating‑point number between 0.0 and 1.0. `random.randint(a, b)` returns a random integer between `a` and `b` inclusive. The former is useful for probabilities or scaling, while the latter is used for discrete choices like dice rolls.

3. Explain the purpose of the if __name__ == "__main__": guard when using modules. How does it relate to the sys module?

Sample AnswerThe guard ensures that code inside it runs only when the module is executed directly, not when it is imported. This is useful for testing or demonstration. It relates to `sys` because the `__name__` attribute is set by the interpreter; when the module is run directly, `__name__` is `"__main__"`, and when imported, it's the module's name. You can also check `sys.argv` to get command‑line arguments within this guard.

4. How does pathlib improve upon os.path for path manipulation? Give an example.

Sample Answer`pathlib` provides an object‑oriented interface, making code more readable and cross‑platform. For example, joining paths with `Path('/home') / 'user' / 'docs'` is clearer than `os.path.join('/home', 'user', 'docs')`. It also provides methods like `.exists()`, `.is_file()`, and `.read_text()`, reducing the need for separate function calls.

5. When would you use re.compile() instead of calling re.search() directly?

Sample AnswerWhen you need to use the same regular expression many times, compiling it with `re.compile()` improves performance because the pattern is parsed and optimized once. This is especially beneficial in loops or large processing tasks where the pattern is reused repeatedly.

Essay Questions

Answer the following questions in 300–500 words each.

6. Compare and contrast the os module and the pathlib module for file system operations. Discuss their respective strengths and weaknesses, and explain when you would choose one over the other.

Suggested outline:

7. Discuss the importance of regular expressions in text processing. Provide two real‑world scenarios where re would be the ideal tool, and explain the potential pitfalls of using regular expressions for complex tasks.

Suggested outline:

Research Questions

These questions require additional research beyond the tutorial content.

8. Research the difference between datetime and pytz (or zoneinfo in Python 3.9+). Why is timezone handling important, and how does the standard library address it?

Sample Answer`datetime` provides basic date and time functionality but does not handle time zones robustly. `pytz` (and the built‑in `zoneinfo` from Python 3.9) provide timezone definitions and allow correct conversion between time zones. Proper timezone handling is crucial for applications that deal with users across multiple time zones, scheduling, and logging timestamps accurately. Using `zoneinfo` is now the recommended approach.

9. Investigate the subprocess module. How does it improve upon os.system()? Provide an example of using subprocess to capture the output of a system command.

Sample Answer`subprocess` gives you more control over executing external commands, including capturing stdout/stderr, providing input, and handling errors. Unlike `os.system()`, it does not use a subshell by default, which is more secure. For example, `subprocess.run(['ls', '-l'], capture_output=True, text=True)` runs `ls -l` and captures its output as a string, which you can then process programmatically.

16. Summary of Tutorial 5

This concludes the Tutorial 5. Once you have completed the exercises and homework, you will be comfortable with the most essential modules in the Python Standard Library. You are now ready to move on to Tutorial 6, where we will learn how to write and use your own custom modules!

Previous | Tutorial index | Next