test

Test what you know about Python's math module: square roots, powers, rounding, constants, factorials, logarithms and angle conversion. There are 20 multiple-choice questions, and every answer is explained after you submit. It takes about 5 minutes.

0 of 20 answered

Which module provides many commonly used mathematical functions in Python?

  • math
  • mathematics
  • maths
  • numeric

The standard-library math module provides mathematical functions and constants.

Which statement correctly imports the math module?

  • import math
  • include math
  • using math
  • require math

import math loads the module, and you then call functions such as math.sqrt().

Which function returns the square root of a number?

  • math.sqrt()
  • math.square()
  • math.root()
  • math.sqroot()

math.sqrt(x) returns the square root of x.

What is the output?

import math
print(math.sqrt(25))
  • 5.0
  • 5
  • 25.0
  • 10.0

math.sqrt() always returns a float, so the result is 5.0.

Which function can be used to raise a number to a power?

  • math.pow()
  • math.power()
  • math.raise()
  • math.exponent()

math.pow(x, y) returns x raised to the power y as a float. The ** operator also works.

What is the output?

import math
print(math.pow(2, 3))
  • 8.0
  • 6
  • 9.0
  • 8

2 to the power 3 is 8, and math.pow() returns a float: 8.0.

Which function returns the smallest integer greater than or equal to a number?

  • math.ceil()
  • math.floor()
  • math.round()
  • math.up()

math.ceil() rounds up to the next integer.

What is the output?

import math
print(math.ceil(4.2))
  • 5
  • 4
  • 4.2
  • 5.2

The smallest integer greater than or equal to 4.2 is 5.

Which function returns the largest integer less than or equal to a number?

  • math.floor()
  • math.ceil()
  • math.down()
  • math.lower()

math.floor() rounds down to the previous integer.

What is the output?

import math
print(math.floor(4.9))
  • 4
  • 5
  • 4.9
  • 5.9

The largest integer less than or equal to 4.9 is 4.

Which constant represents the value of pi?

  • math.pi
  • math.PI
  • math.pie
  • math.circle

math.pi is about 3.14159. Names in Python are case-sensitive, so math.PI does not exist.

Which constant represents Euler's number?

  • math.e
  • math.Euler
  • math.euler
  • math.exp_number

math.e is about 2.71828.

Which function calculates the factorial of a non-negative integer?

  • math.factorial()
  • math.fact()
  • math.multiply()
  • math.product()

math.factorial(n) multiplies all whole numbers from 1 up to n.

What is the output?

import math
print(math.factorial(5))
  • 120
  • 25
  • 60
  • 100

5 × 4 × 3 × 2 × 1 = 120.

Which function calculates the natural logarithm of a number?

  • math.log()
  • math.ln()
  • math.natural()
  • math.logarithm()

math.log(x) uses base e by default. Pass a second argument for another base, as in math.log(8, 2).

Which function converts an angle from degrees to radians?

  • math.radians()
  • math.degrees_to_radians()
  • math.angle()
  • math.rad()

math.radians(x) converts degrees to radians.

Which function converts an angle from radians to degrees?

  • math.degrees()
  • math.degree()
  • math.rad_to_degree()
  • math.angle_degrees()

math.degrees(x) converts radians to degrees.

What is the output?

import math
print(math.gcd(12, 18))
  • 6
  • 36
  • 3
  • 2

6 is the greatest number that divides both 12 and 18.

Which function in the math module returns the absolute value of a number?

  • math.fabs()
  • math.absolute()
  • math.abs()
  • math.absolute_value()

math.fabs() returns a float. The built-in abs() also works without importing anything.

Which statement best describes the math module?

  • It provides mathematical functions and constants for numeric calculations
  • It is used only for addition and subtraction
  • It replaces Python's numeric data types
  • It is required to perform every mathematical operation in Python

Basic operators like + and * need no import. The module adds functions and constants for more advanced work.

Python math module quick reference

Function or constantWhat it doesExample
math.sqrt(x)Square rootmath.sqrt(16) gives 4.0
math.pow(x, y)x to the power y (float)math.pow(2, 3) gives 8.0
math.ceil(x)Round up to an integermath.ceil(4.2) gives 5
math.floor(x)Round down to an integermath.floor(4.9) gives 4
math.factorial(n)n factorialmath.factorial(5) gives 120
math.gcd(a, b)Greatest common divisormath.gcd(12, 18) gives 6
math.log(x[, base])Natural log, or log to a basemath.log(8, 2) gives 3.0
math.radians(x) / math.degrees(x)Convert between degrees and radiansmath.radians(180) gives 3.14159…
math.pi, math.eConstants3.14159…, 2.71828…

Frequently asked questions

How do you import the math module?

Write import math at the top of your file, then call functions with the prefix, for example math.sqrt(25).

What is the difference between math.ceil() and math.floor()?

math.ceil() rounds up to the next integer and math.floor() rounds down to the previous one. For 4.5, they return 5 and 4.

Is the math module needed for basic arithmetic?

No. Addition, subtraction, multiplication and division work without it. Use the module for roots, logarithms, trigonometry, factorials and constants like pi.

What is the difference between math.pow() and the ** operator?

math.pow() always returns a float, while 2 ** 3 returns the integer 8 when both numbers are integers.

Keep practicing

See all Python quizzes

Featured Post

Python Math Module Quiz: 20 MCQs with Answers

Python Math Module Interactive Quiz Test your knowledge of Python's math module, mathematical functions, constants, rounding, powe...

Popular Posts