4.2. The Math Module#
4.2.1. Importing Functions#
The math module gives us
access to many useful mathematical functions. But to use the functions we need
to import the math
module, which we will usually do at the top of our
Python script. There are two ways we recommend doing this
Option 1: Import the entire module. We do this if we want to access all of the available functions.
import math
When you run this, it doesn’t seem to do anything. But you’ll see that this
gives us access to all of the functions inside the math
module. In order to
use these functions, you need to indicate that they’re coming from the math
module using:
math.function()
For example,
import math
print(math.cos(0))
Option 2: Import specific functions from the module.
from math import cos
Again, this doesn’t seem to do anything, but you’ll see that it does give us
access to the cos
function. This time, when we use the cos
function we
don’t need to explicitly say it’s coming from the math
library we can just
use the function:
function()
For example,
from math import cos
print(cos(0))
If you want to import multiple functions you can so so by separating each function with a comma.
For example,
from math import cos, sin
print(cos(0))
print(sin(0))
If you try to import a function that does not exist inside the math
module,
you will get an ImportErrror.
4.2.2. Accessing Constants#
In addition to functions, modules may also provide useful constants. For
example the math
module provides \(\pi\), \(e\), \(\tau =
2\pi\), \(\infty\) and nan (not a number). To use these you also have to
import the math
module, but since they are not functions, you don’t need
the brackets.
Import option 1
import math
print(math.pi)
Import option 2
from math import pi
print(pi)
Question 1
Which of the following will import the math
module and give you access to the function exp()
? Select all that apply.
import math
import math.exp
from math import exp
from exp import math
Solution
import math
Valid.
import math.exp
Invalid.
from math import exp
Valid.
import exp from math
Invalid.
Question 2
Suppose you want to import the math
module. Where in your program should you place the import statement?
At the very start
At the very end
Directly before you first use the
math
module
Solution
Solution is locked
Question 3
Look at the available functions in the math module. How would you convert \(2\pi\) radians to degrees?
import math print(math.degrees(2*math.pi))
import math print(math.radians(2*pi))
from math import degrees print(degree(2*pi))
from math import from_rad print(from_rad(2*math.pi))
Solution
Solution is locked
Code challenge: Factorial
Write a program that reads in a number \(n\) from the user and calculates \(n!\). Your program should use the factorial()
function from the math library.
Example 1
n: 4
24
Example 2
n: 10
3628800
Solution
Solution is locked
Code challenge: Calculate The Sine
Write a program that reads in an angle from the user (in radians) and prints the sin of that angle (also in radians) to 2 decimal places.
Example 1
Enter an angle: 1.32
0.97
Example 2
Enter an angle: 0.9
0.78
Solution
Solution is locked