Learn Without Walls
← Back to Module 10

Module 10: Quick Reference

Modules & Imports Cheat Sheet

Import Styles

import math                     # Full module: math.sqrt(16)
from math import sqrt, pi       # Specific items: sqrt(16)
import math as m                # Alias: m.sqrt(16)
from math import sqrt as sr    # Function alias: sr(16)

math Module

Function/ConstantDescriptionExample
math.sqrt(x)Square rootmath.sqrt(25) → 5.0
math.ceil(x)Round upmath.ceil(3.2) → 4
math.floor(x)Round downmath.floor(3.9) → 3
math.piPi constant3.141592653589793
math.pow(x, y)x raised to ymath.pow(2, 3) → 8.0
math.fabs(x)Absolute value (float)math.fabs(-5) → 5.0

random Module

FunctionDescriptionExample
random.randint(a, b)Random int from a to b (inclusive)randint(1, 6) → 4
random.choice(seq)Random item from sequencechoice(["a","b"]) → "b"
random.shuffle(list)Shuffle list in placeReturns None, modifies list
random.sample(seq, k)k unique random itemssample([1,2,3], 2) → [3,1]
random.random()Random float 0.0 to 1.00.7234...

datetime Module

from datetime import datetime
now = datetime.now()
now.year, now.month, now.day, now.hour, now.minute
Format CodeMeaningExample
%Y4-digit year2025
%mMonth (01-12)06
%dDay (01-31)15
%BFull month nameJune
%AFull weekdaySunday
%I12-hour clock02
%H24-hour clock14
%MMinute30
%pAM/PMPM

pip Commands (Terminal)

pip install package_name         # Install a package
pip install package==1.0.0       # Install specific version
pip install --upgrade package    # Upgrade a package
pip uninstall package            # Remove a package
pip list                         # List installed packages
pip freeze > requirements.txt    # Save package list
pip install -r requirements.txt  # Install from file

Module Best Practices

# Use __name__ guard for test code
if __name__ == "__main__":
    # This only runs when file is executed directly
    print("Testing...")

# Import order: stdlib → third-party → your modules
# Place all imports at the top of the file
# Avoid: from module import *