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
- OS Interface:
os,sys,pathlib(covered later). - Date/Time:
datetime,time(covered later). - Data Serialization:
json,csv(covered later). - 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}")