computer4kids is now ComputerLeap. Same free lessons, same team. Read why we changed our name

← All lessons · 🧑‍💻 Coding Academy

📦

🎓

⏱ 25 min

Free

Modules & the Standard Library

Use Python’s built-in toolboxes — random, math, datetime — and organize your own code into modules.

🎯 What you will learn

  • Import modules and functions
  • Use random, math and datetime
  • Know how packages are installed with pip

👋 Let’s get started!

Python comes with “batteries included” 🔋 — hundreds of ready-made modules. Pros don’t reinvent the wheel; they import it.

🪜 Step by step

1. 📥 import

Import a whole module or just what you need.

import math
from random import choice

print(math.sqrt(81))
print(choice(["rock", "paper", "scissors"]))
2. 📅 datetime

Work with dates and times.

from datetime import date
today = date.today()
print("Year:", today.year)
3. 🧰 Your own modules

Any .py file is a module. Save helpers.py, then import helpers in another file. Outside the browser, install community packages with pip install requests.

# helpers.py
def shout(text):
    return text.upper() + "!"

# main.py
from helpers import shout
print(shout("hi"))

🧪 Try it!

Change the code and press ▶ Run to see what happens. You can’t break anything!

Random team picker

import random

players = ["Ana", "Ben", "Cho", "Dev", "Eli", "Fay"]
random.shuffle(players)
team_a = players[:3]
team_b = players[3:]
print("Team A:", ", ".join(team_a))
print("Team B:", ", ".join(team_b))

Dice roller

import random
roll = random.randint(1, 6)
print("You rolled", roll)

📖 Key words

Tap a card to flip it over!

Module
A file of reusable Python code 📄
import
Loads a module into your program 📥
Standard library
Modules that come with Python 🔋
pip
Tool that installs extra packages 📦

🏆 Challenge

Use math.pi to print the area of a circle with radius 2, rounded to 2 decimal places.

Challenge

import math
radius = 2

Knowledge Check Quiz

  1. What prints?
  2. Which module makes random numbers?
  3. How do you install outside packages?
← Previous lesson
Next lesson →