1. Import math

The standard library is a set of modules that ship with Python. A module is a file with functions, constants and other names. You load it with import:

  • import math runs the module once and binds the name math.
  • After importing, you reach its contents with a dot: module.name.
  • import gives you the module itself; math is not a function.

The math module has common numeric helpers:

  • math.sqrt(49) is 7.0; math.floor(4.8) is 4; math.ceil(4.2) is 5.
  • math.pow(3, 4) returns 81.0, while the operator 3 ** 4 returns the int 81.

Coming from JavaScript? math is like the Math object: math.sqrt is Math.sqrt, and math.pi is Math.PI.

#01

Import a module

Import the math module and print its constant pi.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#02

Call a function with the module name

Import math and print the square root of 144 using math.sqrt.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#03

Round down and round up

Import math. Print math.floor(4.8) and then math.ceil(4.2), each on its own line.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#04

Raise to a power

Import math and print math.pow(3, 4).

Print exactly:

Create a free account to save your progress and unlock chapter 2.