Chapter 1 · Section 2 practice
Modules and Imports
Work from question 1 to 10. Edit your Python box, click Run Code, review the result, then click Submit attempt. Reference answers unlock after all ten attempts are submitted. This records completion, not correctness. Each box runs independently. Progress is saved in this browser.
1. Use a standard module
Warm-up. Import math and print sqrt(25) through the module prefix.
Write your answer, then run it.
2. Use an alias
Warm-up. Import math as m and print sqrt(36).
Write your answer, then run it.
3. Import one function
Warm-up. Import sqrt from math and print sqrt(49).
Write your answer, then run it.
4. Rename one function
Build your skills. Import sqrt as root and print sqrt(81).
Write your answer, then run it.
5. Fix the call
Build your skills. With alias m, write a matching call for sqrt(100). Explain the planned mismatch in a comment.
Write your answer, then run it.
6. Explain an installation command
Build your skills. Store !pip install isom2600 --quiet as a string and print it. Explain !, isom2600, and --quiet in comments. Do not install anything here.
Write your answer, then run it.
7. Recognize a submodule
Build your skills. Import matplotlib.pyplot as plt and SciPy stats as stats. Print their actual names using __name__.
Write your answer, then run it.
8. Compare three forms
Challenge. Calculate sqrt(121) using math.sqrt, alias m, and directly imported sqrt. Print each result on its own line.
Write your answer, then run it.
9. Inspect available names
Challenge. Use dir(m) to print whether math contains sqrt and median. Explain in a comment why a module does not supply every possible function.
Write your answer, then run it.
10. Choose setup steps
Challenge. A new Colab runtime lacks isom2600. Print its installation command and import statement as text, in order. Explain the order and why neither is needed in these website boxes.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Use a standard module
import math
print(math.sqrt(25))Expected output
5.0math requires no pip installation.
2. Use an alias
import math as m
print(m.sqrt(36))Expected output
6.0An alias changes the name, not the tools.
3. Import one function
from math import sqrt
print(sqrt(49))Expected output
7.0Call the directly imported name.
4. Rename one function
from math import sqrt as root
print(root(81))Expected output
9.0root refers to the imported function.
5. Fix the call
import math as m
print(m.sqrt(100))
# This import introduces m, not math.Expected output
10.0Match the call to the imported name.
6. Explain an installation command
command = "!pip install isom2600 --quiet"
print(command)
# ! runs a notebook shell command.
# isom2600 is the course helper package.
# --quiet reduces installation messages.Expected output
!pip install isom2600 --quietPrinting a command as text is ordinary Python. Executing !pip is notebook-specific.
7. Recognize a submodule
import matplotlib.pyplot as plt
from scipy import stats
print(plt.__name__)
print(stats.__name__)Expected output
matplotlib.pyplot
scipy.statsThe aliases refer to matplotlib.pyplot and scipy.stats.
8. Compare three forms
import math
import math as m
from math import sqrt
print(math.sqrt(121))
print(m.sqrt(121))
print(sqrt(121))Expected output
11.0
11.0
11.0All three forms access the same function.
9. Inspect available names
import math as m
print("sqrt" in dir(m))
print("median" in dir(m))
# Each module has its own tools; math has no median.Expected output
True
Falsedir lists names exposed by this module.
10. Choose setup steps
print("!pip install isom2600 --quiet")
print("import isom2600")
# Install makes code available; import creates the session name.
# Website examples use supplied packages and small data directly.Expected output
!pip install isom2600 --quiet
import isom2600The package must be available in the environment before importing it.