Stop 06 · Files, Errors & ModulesTopics · 9/9

The Standard Library Tour (os, sys, datetime, random, math)

Part of the Python Roadmap

In short

Python ships with hundreds of ready-made modules, so you rarely start from zero. Knowing five of them well covers a surprising amount of everyday scripting.

Python comes with 'batteries included': os for the operating system, sys for interpreter details and arguments, datetime for dates, random for random choices and math for maths. Check the standard library before installing anything.

The jaadu unfolds a giant pocket knife with many tools fanned out: a tiny clock, a dice, a compass and a ruler.

Think of it like this

A Swiss Army knife in your pocket

You bought the knife for the blade, but it also has scissors, a screwdriver and a tiny ruler. Python's standard library is like that: it comes free with every install, and before you search online for a package, there's often a tool already folded inside.

Watch it happen

Five modules, five jobs

os
sys
datetime
random
math

1/5os talks to the operating system: environment variables, current folder, listing files.

The big ideas

What you need to know

os for environment

os.environ.get('API_KEY') reads settings without hard-coding secrets. os.getcwd() shows where you are.

sys for the script

sys.argv is the list of command-line arguments. sys.exit(1) stops with an error code.

datetime for time

date.today(), timedelta(days=7) and strftime('%d-%m-%Y') cover most date needs.

random and math

random.choice, random.randint and random.shuffle for chance. math.sqrt, math.ceil and math.pi for maths.

See it in code

Walk through the code

python
1import math2import os3import random4import sys5from datetime import date, timedelta6 7print("Python", sys.version_info.major, sys.version_info.minor)8print("Args:", sys.argv[1:])9print("Working in:", os.getcwd())10print("User:", os.environ.get("USER", "unknown"))11 12today = date.today()13exam = date(2027, 3, 1)14print("Days to exam:", (exam - today).days)15print("Next week:", (today + timedelta(days=7)).strftime("%d-%m-%Y"))16 17players = ["Aarav", "Diya", "Kunal", "Sneha"]18print("Captain:", random.choice(players))19random.shuffle(players)20print("Batting order:", players)21print("Dice:", random.randint(1, 6))22 23print(math.sqrt(144))          # 12.024print(math.ceil(7 / 2))        # 4 buses needed25print(round(math.pi * 3 ** 2, 2))  # area of radius-3 circle

1/6All five come with Python. No pip install needed.

One script, five standard modules: interpreter info, environment, dates, random picks and maths.

Save yourself the pain

Common mistakes

Oops

Writing my_list = random.shuffle(my_list) and getting None.

Do this instead

shuffle works in place and returns None. Just call random.shuffle(my_list).

Oops

Using random for passwords or OTPs.

Do this instead

random is predictable. Use the secrets module, such as secrets.token_hex(16), for anything security related.

Oops

Hard-coding an API key in the script.

Do this instead

Store it in an environment variable and read it with os.environ.get('API_KEY').

Your mission

Birthday countdown CLI

~25 min

Quick check

Which call gives a random whole number from 1 to 6 including both ends?

Go deeper

  1. 1Print today's date in DD-MM-YYYY format with datetime.
  2. 2Pick a random winner from a list of names with random.choice().
  3. 3Browse the Python standard library index once and note five modules you didn't know.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge