Answer key · 12 questions

Python Modules and Imports Exercises With Answers

All 12 questions for Python Modules and Imports in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

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.

01 · Multiple choice

What does import math do?

  1. Loads the math module for usecorrect
  2. Prints every function in math
  3. Installs math from the internet
  4. Renames math to module

Answer: Loads the math module for use

Why: The import statement makes the module's names available through the math namespace.

02 · Multiple choice

What appears on the screen?

import math
print(math.floor(3.8))
  1. 3.8
  2. 3correct
  3. 8
  4. 4

Answer: 3

Why: math.floor rounds down to the nearest integer.

03 · Multiple choice

Which line imports only sqrt from math?

  1. import math.sqrt
  2. from math import sqrtcorrect
  3. from sqrt import math
  4. import math as sqrt

Answer: from math import sqrt

Why: A from-import brings one name into the current namespace.

04 · Multiple choice

What is printed?

import statistics as stats
print(stats.mean([2, 4, 6]))
  1. 2
  2. 6
  3. 12
  4. 4correct

Answer: 4

Why: The alias stats refers to the statistics module, and mean([2, 4, 6]) is 4.

05 · Fill in the blank

Complete the import statement.

____ math
print(math.ceil(2.1))

Answer: import

Why: import math loads the module before math.ceil is called.

06 · Fill in the blank

Complete the from-import so sqrt works.

from math ____ sqrt
print(sqrt(25))

Answer: import

Why: The form is from module import name.

07 · Fill in the blank

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.

08 · Fill in the blank

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.

09 · Predict output

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.

10 · Predict output

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.

11 · Predict output

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.

12 · Predict output

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.

Practice this lesson