← Back to Blog

Modules — The Full Picture

You've covered the modules that matter most at this level. Before moving on, here's everything in one place.

// What you already knew

import random
random.randint(1, 10)        # random integer
random.choice(lst)           # random element
random.shuffle(lst)          # shuffle in place

import math
math.sqrt(16)                # 4.0
math.floor(3.7)              # 3
math.ceil(3.2)               # 4
math.pi                      # 3.14159...
math.pow(2, 8)               # 256.0

// datetime

from datetime import datetime, date, timedelta

datetime.now()               # current date and time
date.today()                 # current date only
dt.strftime("%Y-%m-%d %H:%M")   # datetime → string
datetime.strptime(s, fmt)    # string → datetime
date.today() + timedelta(days=7) # one week from today
end - start                  # timedelta between two dates

// statistics

import statistics

statistics.mean(lst)         # average
statistics.median(lst)       # middle value
statistics.mode(lst)         # most frequent (error if all unique)
statistics.multimode(lst)    # all most frequent, no error
statistics.stdev(lst)        # standard deviation

// time

import time

time.sleep(2)                # pause 2 seconds
time.sleep(0.5)              # pause 0.5 seconds

start = time.perf_counter()
# ... code ...
end = time.perf_counter()
print(end - start)           # elapsed time

// collections

from collections import Counter, defaultdict

Counter(lst)                 # count occurrences
counter[key]                 # 0 if missing — no KeyError
counter.most_common(n)       # top n as (value, count) tuples

defaultdict(list)            # missing key → []
defaultdict(int)             # missing key → 0
defaultdict(set)             # missing key → set()

// string

import string

string.ascii_lowercase       # abcdefghijklmnopqrstuvwxyz
string.ascii_uppercase       # ABCDEFGHIJKLMNOPQRSTUVWXYZ
string.ascii_letters         # all letters
string.digits                # 0123456789
string.punctuation           # all punctuation

// import patterns

import math                  # whole module — access with math.sqrt()
from math import sqrt        # one thing — use directly
from datetime import datetime, date  # multiple things

// Your own modules

import utils                 # whole module — access with utils.function()
from utils import greet      # one function — use directly

# utils.py must be in the same folder as the importing script

if __name__ == "__main__":
    pass                     # runs only when file is executed directly
                             # not when imported

That's the standard library at beginner level. Everything here is already on your machine — no installation needed.

The cheatsheet above is yours to download and keep.

[ login to bookmark ] // copied! 20 views · 1 min
// resources
Cheatsheet Modules — The Full Picture
← prev Modules Mini Project — Write Once, Import Everywhere next → The Toolkit Is Stocked
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.