Reading preferences
Concept

Importing Python Modules

Reuse a named collection of Python tools from the standard library.

python, modules, import, standard library

Use a toolbox

You do not need to make a ruler every time you measure a page. Python also provides tools you can reuse. A module groups code under a name. Python's standard library includes modules supplied with a normal Python installation; additional packages are a separate topic.

import math gives access to the math module. The dot in math.sqrt selects the sqrt function from that module. This makes its origin clear.

Python illustration

import math

area = 9
side = math.sqrt(area)
print(side)
print(math.floor(2.8))

Prepared output — not live execution:

3.0
2

sqrt returns a floating-point result here. A negative real input raises ValueError; the real-number square-root example does not handle it. Importing code can execute module-level instructions, so use trusted sources.

Keep names clear

from math import sqrt imports that function by name; the example above uses math.sqrt to show where it comes from. Avoid naming your own file math.py, because a local file can interfere with finding the intended module. Packages, dependency installation, and environments have more detail in later lessons; this example requires no third-party package.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What is a Python module?
2. What does import math make available?
3. How do you call sqrt after import math?
4. What should you consider before importing unfamiliar code?

References and further reading