Python Modules
csv and json were modules. A module is a group of names, and import is how you bring those names into the program. This page brings in math. It does not write a file, and it does not build a package of several files.
import math
print(math.sqrt(9))
The screen shows 3.0. sqrt lives inside math, so the call is math.sqrt. You hand it the number 9. What comes back is 3.0, not the integer 3. The point in 3.0 belongs to the result. sqrt can give a fraction, and here that fraction is zero. Write sqrt(9) with no math in front and the program stops. The name of that stop is NameError, because sqrt was never brought in on its own. That line is not in this program. None is not written there, and the program does not go on.
from math import sqrt
print(sqrt(9))
The screen shows 3.0 again. from brought the single name sqrt in, so you do not write math in front. The name math itself was not imported. math.sqrt(9) after only that from line stops as NameError. That line is not in this program either. Same answer, two spellings.
The next page is a package of several files. This page only brings names in from one module, math.
Two ways to bring sqrt in
import math and then math.sqrt(9) shows 3.0. from math import sqrt and then sqrt(9) shows 3.0. A name that was not imported stops as NameError. No file is written. The next page is a package of several files.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top