Alex had a great time learning about the datetime
module in Python and was eager to explore another useful module – the math
module. The math
module provides access to mathematical functions and constants in Python. With the math
module, Alex could perform advanced mathematical operations that were not available with basic arithmetic operators.
Alex started by importing the math
module.
import math
He then used the math.sqrt()
function to calculate the square root of a number.
num = 25 square_root = math.sqrt(num) print("The square root of", num, "is:", square_root)
The output was:
The square root of 25 is: 5.0
Next, Alex used the math.pi
constant to calculate the circumference of a circle with a radius of 5.
radius = 5 circumference = 2 * math.pi * radius print("The circumference of a circle with a radius of", radius, "is:", circumference)
The output was:
The circumference of a circle with a radius of 5 is: 31.41592653589793
Alex also used the math.pow()
function to raise a number to a power.
num = 2 power = 3 result = math.pow(num, power) print(num, "raised to the power of", power, "is:", result)
The output was:
2 raised to the power of 3 is: 8.0
Alex was amazed at the power and flexibility of the math
module. He realized that with the math
module, he could perform complex mathematical calculations with ease. He also learned that the math
module had many other useful functions and constants, such as trigonometric functions, logarithmic functions, and mathematical constants, that he could use in his future projects.