Python Math


Python has a built-in math module that gives access to many mathematical functions like square roots, trigonometry, logarithms, rounding, and more.


🔹 Importing the Math Module

Before using math functions, you need to import the module:

import math

🔹 Common Math Functions

Function Description
math.sqrt(x) Square root of x
math.pow(x, y) x raised to the power y
math.floor(x) Rounds down to nearest integer
math.ceil(x) Rounds up to nearest integer
math.pi Returns the value of π (3.14159)
math.e Euler's number (2.718...)
math.fabs(x) Absolute value (float)
math.factorial(x) Factorial of x

🔹 Example: Square Root

import math
print(math.sqrt(16))  # Output: 4.0

🔹 Example: Power

print(math.pow(2, 3))  # Output: 8.0

🔹 Example: Rounding

print(math.floor(3.7))  # Output: 3
print(math.ceil(3.2))   # Output: 4

🔹 Example: Factorial

print(math.factorial(5))  # Output: 120

🔹 Trigonometric Functions

Function Description
math.sin(x) Sine of x (in radians)
math.cos(x) Cosine of x (in radians)
math.tan(x) Tangent of x (in radians)
math.radians(x) Converts degrees to radians
math.degrees(x) Converts radians to degrees

🔹 Example: Trigonometry

angle = math.radians(90)
print(math.sin(angle))  # Output: 1.0

Practice Questions

Q1. Write a Python program to print the value of math.pi.

Q2. Write a Python program to find the square root of 121 using math.sqrt().

Q3. Write a Python program to raise 3 to the power 4 using math.pow().

Q4. Write a Python program to use math.ceil() on 2.1 and display the result.

Q5. Write a Python program to use math.floor() on 9.8 and display the result.

Q6. Write a Python program to convert 90 degrees to radians using math.radians().

Q7. Write a Python program to find the cosine of 0 radians using math.cos().

Q8. Write a Python program to calculate the factorial of 7 using math.factorial().

Q9. Write a Python program to find the absolute value of -9.5 using math.fabs().

Q10. Write a Python program to convert 3.14 radians to degrees using math.degrees().


Python Math Quiz

Q1: Which module provides advanced math functions in Python?

A. calc
B. math
C. random
D. numeric

Q2: What does math.sqrt(49) return?

A. 7
B. 6
C. 49
D. 14

Q3: What is the use of math.floor(5.9)?

A. Rounds up to 6
B. Returns 5
C. Returns 6
D. Error

Q4: Which constant gives the value of π?

A. math.pi
B. math.pi()
C. math.circle
D. math.π

Q5: What does math.pow(2, 4) return?

A. 16
B. 8
C. 4
D. 2

Q6: What does math.fabs(-4.5) return?

A. -4.5
B. 4.5
C. 0
D. 5

Q7: What is the factorial of 5?

A. 120
B. 25
C. 60
D. 20

Q8: Which function converts degrees to radians?

A. math.radians()
B. math.degrees()
C. math.toRadians()
D. rad()

Q9: What is math.sin(math.radians(90))?

A. 0
B. 0.5
C. 1
D. -1

Q10: What is returned by math.ceil(2.2)?

A. 2
B. 3
C. 2.2
D. 1

Go Back Top