A module is a file containing Python code. Python ships with a large standard library of modules.
import math: then use math.sqrt().
from datetime import date: import one name.
import statistics as st: give it a shorter alias.
Your own file helpers.py can be imported with import helpers.
Install third-party packages with pip install package-name.
💡 Key Takeaways: Python Modules
Mastering Python Modules helps you write clean and reliable Python code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
Python Modules Code Example
Python
import math
import statistics as st
from datetime import date
from collections import Counter
print(math.sqrt(144), math.pi)
print("Mean:", st.mean([70, 85, 90]), "Median:", st.median([70, 85, 90]))
print("Today:", date.today())
print(Counter("mississippi").most_common(2))
Free sign-in required · Google login available
Python Modules Practice Exercises
Check what you learned in “Python Modules”.
/ 3 solved
Fill in the blank
Import the math module.
math
✓ Correct!Not quite. Try again.
Multiple choice
How do you import only sqrt from math?
✓ Correct!Not quite. Try again.
Code challenge
Import math and print the square root of 81.
Write the code in the live editor. Your answer is checked automatically.