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 mathruns the module once and binds the namemath.- After importing, you reach its contents with a dot:
module.name. importgives you the module itself;mathis not a function.
The math module has common numeric helpers:
math.sqrt(49)is7.0;math.floor(4.8)is4;math.ceil(4.2)is5.math.pow(3, 4)returns81.0, while the operator3 ** 4returns the int81.
Coming from JavaScript? math is like the Math object: math.sqrt is Math.sqrt, and math.pi is Math.PI.
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.
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.
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.
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.