This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python Modules and Imports, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
What does import math do?
Answer: Loads the math module for use
Why: The import statement makes the module's names available through the math namespace.
What appears on the screen?
import math
print(math.floor(3.8))Answer: 3
Why: math.floor rounds down to the nearest integer.
Which line imports only sqrt from math?
Answer: from math import sqrt
Why: A from-import brings one name into the current namespace.
What is printed?
import statistics as stats
print(stats.mean([2, 4, 6]))Answer: 4
Why: The alias stats refers to the statistics module, and mean([2, 4, 6]) is 4.
Complete the import statement.
____ math
print(math.ceil(2.1))Answer: import
Why: import math loads the module before math.ceil is called.
Complete the from-import so sqrt works.
from math ____ sqrt
print(sqrt(25))Answer: import
Why: The form is from module import name.
Complete the alias for the random module.
import random ____ rnd
print(rnd.randint(1, 1))Answer: as
Why: import random as rnd makes the module available through the shorter name rnd.
Complete the statistics call that calculates the average.
import statistics
print(statistics.____([2, 4, 6]))Answer: mean
Why: statistics.mean returns the arithmetic mean of the values.
What is the output?
import math
print(math.sqrt(64))Answer: 8.0
Why: The square root of 64 is 8.0, and sqrt returns a float.
What does this code print?
from math import pow
print(pow(2, 3))Answer: 8.0
Why: pow(2, 3) means 2 to the power of 3, which is 8.0.
What does the program output?
import math
print(math.ceil(4.2))Answer: 5
Why: ceil rounds up to the next integer, so 4.2 becomes 5.
What appears on the screen?
from statistics import mean
print(mean([2, 4, 6]))Answer: 4
Why: The sum is 12 and there are 3 values, so the mean is 4.
Try the questions first
Open the practice page for Python Modules and Imports and answer the same 12 questions with instant feedback.