Back to course

The Standard Library: Introduction to Built-in Tools

Python Programming: The 0 to Hero Bootcamp

The Python Standard Library (STL)

Python is often called 'batteries included' because the Standard Library provides a huge collection of modules for common tasks, such as file I/O, networking, data serialization, math, and date handling, without needing to install external dependencies.

Key Categories in the STL

  1. OS Interface: os, sys, pathlib (covered later).
  2. Date/Time: datetime, time (covered later).
  3. Data Serialization: json, csv (covered later).
  4. Math: math, random.

Example: The random Module

Used for generating pseudo-random numbers, crucial for simulations, games, or simple sampling.

python import random

1. Generate a random floating point number between 0.0 and 1.0

rand_float = random.random()

2. Get a random integer within a range (inclusive)

rand_int = random.randint(1, 100)

3. Choose a random element from a sequence

choices = ['rock', 'paper', 'scissors'] computer_choice = random.choice(choices)

print(f"Random number: {rand_int}") print(f"Computer chose: {computer_choice}")