Check your understanding of the previous reading: modules, packages and the import statement.

Question 1

True or false: import is a function that takes in the name of a module and loads the functions, variables, and other definitions.

Select one answer, then check.

  • Correct. import does load a module's functions, variables and other definitions, but it isn't a function: it's a keyword that starts an import statement. You write import math, without parentheses. Trying to call it like a function, as in import(math), is a SyntaxError.
  • Not quite. The second half is right: importing loads the module's definitions. But import is a keyword that starts a statement, not a function. A function is called with parentheses, like print("hi"); an import is written import math, and import(math) is a SyntaxError.

Question 2

Review the documentation for the built-in math module. Which of the following are functions available from it?

Select all answers that apply, then check.

  • Correct. math.sqrt computes a square root: math.sqrt(16) is 4.0.
  • Correct. math.log computes a logarithm. With one argument it's the natural logarithm (base e); a second argument picks the base, so math.log(8, 2) is 3.0.
  • Not a function in math. Only two bases get a function of their own, log2 and log10. For base 4 you write math.log(x, 4).
  • Correct. math.log10 computes the base-10 logarithm: math.log10(1000) is 3.0.
  • Not a function in math. The exponential function is called math.exp. To raise a number to a power, use the ** operator (or math.pow).
  • Not a function in math. You add numbers with the + operator; the math module has no add.

Question 3

Which of the following is the best description of this line of code?

from math import sqrt as square_root

Select one answer, then check.

  • Correct. from math import sqrt loads sqrt from the math module, and as square_root gives it a new name in your program. Afterwards you call it as square_root(16); the name sqrt itself is not defined.
  • Not quite. This is backwards: math is the module and sqrt the function. The word right after from is always the module.
  • Not quite. The two names are swapped: the original name comes right after import, and the new name comes after as.
  • Not quite. Only one function is loaded. math has no square_root function; square_root is the new name given to sqrt.

Question 4

A contains , which in turn can provide functions, , and more after being imported.

Word bank — not all words are used