Using modules

Introduction

A module is a file of ready-made code that you can use instead of writing everything yourself. Python ships with hundreds of them, called the standard library. This lesson uses two of the most common: math and random.

Good programmers reuse tested code rather than writing everything themselves. Python's standard library handles dates, maths, files and much more, so that you can spend your time on the part of the problem that is really yours.

How it works

from … import

from math import gcd, factorial

print(gcd(48, 180))
print(factorial(5))
Open in compiler

from math import gcd, factorial brings just those two names into your program, so you can call gcd(48, 180) without the math. prefix. Use this form when you need only a few tools and their names are clear.

Random numbers you can repeat

import random

random.seed(7)
dice = random.randint(1, 6)
coin = random.choice(["heads", "tails"])
print(dice, coin)
Open in compiler

random.randint(1, 6) picks a whole number from 1 to 6, both included, and random.choice(list) picks one item. random.seed(7) fixes the starting point, so the "random" results are the same every time you run it: here 3 heads. That makes programs that use randomness testable. Remove the seed line to get different results on each run.

Syntax

import math
math.sqrt(16)

from math import gcd
gcd(12, 18)

import random
random.seed(1)
random.randint(1, 6)

Example

import math

r = 3
area = math.pi * r * r
print(round(area, 2))
print(math.sqrt(144), math.floor(7.9), math.ceil(7.1))
Open in compiler

import math loads the module once; after that, its tools are reached with a dot: math.pi, math.sqrt(144). In the variables panel, math appears as a variable too, because a module is just another value with a name.

ToolGivesExample
math.piπ, about 3.14159math.pi * r * r
math.sqrt(x)square root, as a floatmath.sqrt(144) is 12.0
math.floor(x)round downmath.floor(7.9) is 7
math.ceil(x)round upmath.ceil(7.1) is 8
round(x, 2)built-in, not from math: round to 2 places28.27

Line by line

  1. import math loads the module and gives it a name.
  2. r = 3 is the radius.
  3. math.pi * r * r uses the module's value of pi.
  4. round(area, 2) is a built-in function, and it keeps two decimals.
  5. The last line shows three more tools from the module.

Expected result

28.27
12.0 7 8

In the real world

Almost every real program imports something. Beyond the standard library, thousands of free packages handle tasks such as web servers, data analysis and machine learning, and using them well is a core skill.

Common mistakes

  • Naming your own file math.py or random.py. Python then imports your file instead of the real module.
  • Forgetting the prefix: after import math, plain sqrt(9) is a NameError; write math.sqrt(9).
  • random.randint(1, 6) includes 6, but range(1, 6) does not. The two count differently.

Best practices

  • Put imports at the top of the file.
  • Use import math when you use many tools, and from for a few clear names.
  • Use a seed when you need to test code that uses randomness.

Summary

A module is ready-made code that you load with import. Its tools are reached with a dot, or brought in by name with from. math handles calculations, and random makes choices that a seed can make repeatable.

Practice

Simulate rolling two dice 10 times with random.seed(1) and print each total. Then count how often the total was 7.

Quick check

1. After import math, how do you call the square root function?
2. Which values can random.randint(1, 3) return?
3. Why call random.seed(7)?

Key takeaways

  • Do not write what the standard library already provides.
  • Never name your own file after a module you use.
  • randint includes both ends, unlike range.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.