✦ Python Intermediate & OOP Intermediate Python

L2 · Concept 01 · Modules, Packages & Environments

Use code that Python already has.
Import a module.

Python comes with useful, tested code. Learn how an import gives your program access to it without writing everything yourself.

Learning goalExplain what a module is and use import, from ... import ..., and a simple alias to access code from Python’s standard library.
01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

A module is a Python file whose code you can use.

Instead of writing every useful feature yourself, you can import a module that already contains it. For now, we will use modules from Python’s standard library: helpful code that comes with Python.

import math

print(math.sqrt(25))

import math makes the math module available. sqrt is a function inside that module, so math.sqrt(25) calls that particular function.

5.0

The dot helps Python—and a reader of your code—see where sqrt comes from. The standard library has many other modules, so later you will not need to reinvent every tool you use.

↳

A useful random example: random.randint(1, 10) returns a whole random number from 1 through 10. The two limits are both included, so a different run can print a different number.

Try a random number
import random

broj = random.randint(1, 10)
print(broj)

random is another standard-library module. Run this more than once to see that the result can change.

Three ways to import
import math
print(math.sqrt(16))

from math import sqrt
print(sqrt(16))

import math as m
print(m.sqrt(16))

The first form keeps the module name. The second imports only sqrt, so it can be called directly. The third gives math the shorter alias m; an alias is optional, not something you must always use.

Keep names clear
from math import *  # Usually avoid this

This imports many names at once, which can make it unclear where a name came from. Prefer a clear module name or import the one function you need.

Read an import step by step

  1. 1. Import the moduleWrite import math before using code from math.
  2. 2. Name the moduleWrite math. to say which module you are using.
  3. 3. Call a functionWrite the function name and its parentheses, such as math.sqrt(25).
  4. 4. Reuse helpful codePython’s standard library offers modules now; later, you can make your own modules too.

Common mistakes

  • Forgetting the importPython must see import math before you use math.sqrt(...).
  • Forgetting the module nameAfter import math, write math.sqrt(...), not just sqrt(...).
  • Mixing import stylesAfter from math import sqrt, call sqrt(...). After import math as m, call m.sqrt(...).
  • Assuming random always gives a new visible resultA random result can repeat by chance, even though Python chooses it again each run.

Python Playground

Loading Python runtime…

Run the examples, then change the values. Everything runs in this browser Playground; no local files or VS Code workflow are needed for this lesson.

importing_modules.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

The function is imported directly, so check whether the call still needs math.

from math import sqrt

print(sqrt(16))
Choose the output

Practice independently

Mini Challenge — Random number and square root

Use two standard-library modules to generate a number, calculate its square root, and print both values.

Your goalImport <code>random</code> and <code>math</code>. Generate a whole number from 1 to 20, save it, calculate its square root, then print the original number and its square root.

random_square_root.py

Optional hints
  • Use random.randint(1, 20) to create the number.
  • Use math.sqrt(broj) after importing math.
  • An f-string can print both values, for example print(f"Broj: {broj}").
Ask Sloby for a hint
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.