Modules and environments
Imports and the standard library
Import a module and call its named functions.
Lesson 39 of 63
Reading: about 1 min
Lesson + practice: about 13 min
Difficulty: Intermediate
On this page5 sections
A module groups related names. import math binds the module name; math.ceil then makes the source of the function clear. The standard library ships with Python.
Worked example
import math
print(math.ceil(2.4))
print(math.floor(2.4))3 2
Why this works
ceil rounds upward to the next integer and floor rounds downward. Module-qualified names help avoid collisions with your own names.
Another case
from collections import Counter
print(Counter(["py", "sql", "py"])["py"])2
Practice
Import math and print math.sqrt(81). Predict the result before running it.
Check the practice result
Reveal answer
math.sqrt(81) returns 9.0, a float.
Try another case
What do math.ceil(-2.4) and math.floor(-2.4) return?
Check your answer
Reveal answer
-2 and -3. ceil rounds up towards positive infinity, and floor rounds down towards negative infinity.
Your progress
Reveal the check answer or reach the end to complete this lesson.