TalentJR

🐍 Python for kids · Class 7 · Lesson 15 of 18 · Free

Modules & Imports – Use Python's Toolboxes

⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in

In this lesson you will

  • Import a module and use its functions with the dot (.) notation.
  • Use from-import and aliases to write shorter code.
  • Explain how your own .py files become modules for bigger projects.

A carpenter does not build a new hammer for every job. The tools wait in a toolbox. Python also has toolboxes full of ready-made functions. They are called modules, and import opens them.

What is a module in Python?

A module is a Python file full of useful functions and values. Python comes with hundreds of them.

They are not loaded automatically, which keeps programs light. You ask for one with import.

After import math, you use its tools with a dot: math.sqrt() means "the sqrt tool from math".

🎬 How Python thinks — step 1 of 7Python is about to run line 1
▶import math
2
3side = math.sqrt(49)
4print("Side of the square:", side)
5print("Pi is about", math.pi)
6area = math.pi * 7 ** 2
7print("Circle area:", round(area, 2))
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Side of the square: 7.0
Pi is about 3.141592653589793
Circle area: 153.94

💡 Put import lines at the top of the program. Then every reader sees which toolboxes you use.

What useful functions are in the math module?

math.ceil() rounds up to the next whole number, and math.floor() rounds down.

These are perfect for real problems. If 45 students go on a trip and a van holds 8, how many vans?

45 / 8 is 5.625, but you cannot book 0.625 of a van. So you round up.

🐍 Python — you can edit this code
Output
Vans needed: 6
Full boxes of 12 from 50 laddoos: 4
Hypotenuse of a 3-4 triangle: 5.0

How do I use from import and import as?

from math import sqrt brings just sqrt into your program. Then you can write sqrt() without math. in front.

import statistics as st gives a module a short nickname, called an alias. Long names become quick to type.

The statistics module finds the mean, median and mode of a list of numbers.

🎬 How Python thinks — step 1 of 8Python is about to run line 1
▶from math import sqrt
2import statistics as st
3
4print(sqrt(144))
5marks = [72, 85, 90, 85, 60]
6print("Mean:", st.mean(marks))
7print("Median:", st.median(marks))
8print("Mode:", st.mode(marks))
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
12.0
Mean: 78.4
Median: 85
Mode: 85

💡 Mean is the average, median is the middle value after sorting, and mode is the most common value.

How do I use the random module?

The random module makes surprises, which games need. random.randint(1, 6) gives a whole number from 1 to 6, like a dice.

random.choice() picks one item from a list. random.shuffle() mixes up a list in place.

Use random for games and quizzes, so every round feels different.

🐍 Python — you can edit this code
Output
You rolled: 5
Today's snack: dhokla
Shuffled: ['idli', 'dhokla', 'samosa', 'poha']

💡 The output changes every time you run this code. That is the whole point of random!

What is in the string module?

The string module holds ready-made strings of letters, digits and symbols. You do not have to type the alphabet yourself.

This helps with jobs like checking whether a password has a digit and a symbol.

any() is True if at least one item in the loop is True.

🐍 Python — you can edit this code
Output
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
Has a digit: True
Has a symbol: True

How do I make my own module in Python?

Any .py file you write is also a module. Imagine a file called school_tools.py with the two functions below.

Another file in the same folder, say main.py, can then write import school_tools. It uses school_tools.grade(91), just like math.sqrt().

Our page runs only one file, so here both parts sit together. On your computer, you would split them into two files.

Splitting code this way keeps big projects tidy. Each file does one kind of job, and teammates can share them.

🐍 Python — you can edit this code
Output
A B
84.6 %

💡 Never name your own file math.py or random.py. Python would import your file instead of the real module.

🤔 Predict the output

Think like Python: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

import math
print(math.ceil(7.1), math.floor(7.9))

Question 2. What does this print?

from statistics import median
print(median([9, 2, 7]))

Question 3. What does this print?

import string
print(len(string.digits), string.ascii_lowercase[:3])

🧩 Your challenge

Make a class result helper. Ask for 5 marks, then print the mean (rounded to 1 decimal place), the median, and the square root of the highest mark rounded to 2 places. Use the statistics and math modules.

Expected output (with the inputs given):
Mark: 78
Mark: 64
Mark: 91
Mark: 85
Mark: 70
Mean: 77.6
Median: 78
Root of top mark: 9.54
🐍 Python — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

print(math.sqrt(25))

NameError: name 'math' is not defined

✅ Right

import math
print(math.sqrt(25))

Python does not know the name math until you import it. Always import before you use a module.

❌ Wrong

import math
print(sqrt(25))

NameError: name 'sqrt' is not defined

✅ Right

import math
print(math.sqrt(25))

import math loads the module, but its tools still need the math. prefix. Or write from math import sqrt.

❌ Wrong

import Math
print(Math.pi)

ModuleNotFoundError: No module named 'Math'

✅ Right

import math
print(math.pi)

Module names are case-sensitive. The real module is math, all lowercase.

📌 Remember

  • A module is a file of ready-made functions; import loads it.
  • Use a dot to reach a module's tools, like math.sqrt() or random.choice().
  • from module import name lets you skip the prefix; import module as nick gives an alias.
  • math, random, statistics and string come with every Python install.
  • Your own .py file is a module too, imported by its file name without .py.
  • Never name your file after a real module, like math.py.

Finished the quiz and the challenge?

Questions students ask

How many modules does Python have?

The standard library has over 200 modules, from dates to web pages. Thousands more can be installed with a tool called pip.

Should I use import math or from math import *?

Prefer import math or from math import sqrt. The star imports every name and can quietly overwrite your own variables.

Why does my own module run its code when I import it?

Importing runs the whole file once. Put test code under if __name__ == "__main__": so it runs only when you start that file directly.

Where can I see what a module contains?

Run print(dir(math)) to list its names, or help(math.ceil) to read about one function.