Python Modules — Python Technical Interview Questions & Answers
Python Modules
Python Modules
- A module is a .py file you import and reuse. import math then math.sqrt(9) is 3.0. from math import sqrt lets you call sqrt(9) directly. Your own utils.py can be a module if it sits on the path. A package is a folder of modules (often with __init__.py).
- import math brings the whole module namespace. from math import sqrt brings one name. If two modules have sqrt, import module keeps them apart: math.sqrt vs other.sqrt. Don’t star-import in real code.
- Python Modules — output: 7.0 then 3.14159…. math is a real stdlib module.
Technical Interview Tip: import vs from-import difference.
Code Examples for Python Modules
Example
# Modules
import math
print(math.sqrt(49))
print(math.pi)
Python Modules — output: 7.0 then 3.14159…. math is a real stdlib module.
How to Explain Python Modules to an Interviewer
Explain Modules as if you are teaching a junior — definition, then one tiny script.
.py file you import. import x vs from x import y.
What does the example print, and why?
Python Modules — output: 7
What mistake do freshers make with Modules?
Your file can be a module.