Course 2.2Lesson 1 of 5
A module is a toolbox of ready-made code. import math brings the toolbox in, and math.sqrt uses a tool with a dot. The math module has sqrt, pi, ceil, floor, factorial, gcd, hypot, trigonometric functions that work in radians, and isclose for comparing decimal numbers. from math import sqrt brings in one tool and import math as m gives the toolbox a shorter name. A tool may refuse a value with a ValueError. A module name that does not exist gives a ModuleNotFoundError, and a tool name that does not exist gives an AttributeError.
Try the code and the simulations beside the lesson.
After this lesson you can
math,import math, from math import sqrt and import math as m, andSuppose that you need a square root. You could write a long function with a loop. Or you could use a tool that experts have already made, tested, and made fast. Python keeps such tools in modules. A module is a toolbox.
To use a toolbox, you import it. Then you reach a tool with a dot: math.sqrt(144). The dot says: "the tool sqrt that lives in the toolbox math".
Why does Python not bring every tool in from the start? Because a program that imports only what it needs starts faster, and the names do not clash. You ask for the toolbox when you need it.
Predict first: what will each line show? Then run it.
math.sqrt(144) is 12.0. The answer is a decimal number, even for a perfect square.math.pi is the number pi. round(math.pi, 4) gives 3.1416.math.floor(7.9) rounds down to 7, and math.ceil(7.1) rounds up to 8. The names mean "the floor" and "the ceiling".math.factorial(5) is .math.gcd(12, 18) is the greatest common divisor, 6.math.hypot(3, 4) is the long side of a right triangle with sides 3 and 4: 5.0.| Way | How you use the tool | When |
|---|---|---|
import math | math.sqrt(2) | the usual way. It is clear where the tool comes from. |
from math import sqrt | sqrt(2) | a few tools that you use many times |
import math as m | m.sqrt(2) | a long module name that you write often |
A fourth way, from math import *, brings in every tool, with no module name. Avoid it. You will not know where a name comes from, and a name can silently replace one of your own.
For a positive number, floor and trunc agree. Only below 0 do they part: math.floor(-2.3) is -3, while math.trunc(-2.3) is -2.
Two mistakes with modules are common. Both give a clear message.
NameError: name 'math' is not defined.sin, cos and tan.==. Use isclose.math.py. Python imports your file instead of the real module. Choose another name.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
import math brings the toolbox. math.sqrt(x) uses a tool.ceil rounds up. floor rounds down. trunc cuts.sin and cos use radians. isclose compares decimal numbers.ModuleNotFoundError is a wrong module name. AttributeError is a wrong tool name.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
math.floor(7.9)?math.ceil(7.1)?sqrt?0.1 + 0.2 == 0.3 false?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.