-
Notifications
You must be signed in to change notification settings - Fork 0
Math Functions
This section covers:
✅ Python’s built-in math functions 🎲
✅ Using the math module for advanced calculations 📊
✅ Common math functions with examples 💡
✅ How to avoid common math-related errors 🚨
Python has many built-in functions for mathematical calculations. Let’s explore! 🔍
| Function | Description | Example | Output |
|---|---|---|---|
abs(x) |
Absolute value | abs(-10) |
10 |
round(x, n) |
Rounds a number to n decimal places |
round(3.14159, 2) |
3.14 |
max(x, y, ...) |
Returns the largest value | max(10, 20, 5) |
20 |
min(x, y, ...) |
Returns the smallest value | min(10, 20, 5) |
5 |
pow(x, y) |
Power calculation (same as x ** y) |
pow(2, 3) |
8 |
🔹 Absolute value (abs())
print(abs(-10)) # 10
print(abs(5)) # 5🔹 Rounding numbers (round())
print(round(3.14159, 2)) # 3.14
print(round(4.56789, 3)) # 4.568🔹 Finding max and min (max(), min())
print(max(10, 20, 5)) # 20
print(min(10, 20, 5)) # 5🔹 Power calculation (pow())
print(pow(2, 3)) # 8 (same as 2 ** 3)
print(pow(5, 2)) # 25For advanced math functions, Python has a built-in math module. First, import it:
import math| Function | Description | Example | Output |
|---|---|---|---|
math.sqrt(x) |
Square root | math.sqrt(16) |
4.0 |
math.ceil(x) |
Rounds up | math.ceil(4.2) |
5 |
math.floor(x) |
Rounds down | math.floor(4.8) |
4 |
math.factorial(x) |
Factorial (x!) | math.factorial(5) |
120 |
math.log(x) |
Natural log (base e) |
math.log(10) |
2.302 |
math.log10(x) |
Log base 10 | math.log10(1000) |
3.0 |
math.sin(x) |
Sine function | math.sin(math.pi/2) |
1.0 |
math.cos(x) |
Cosine function | math.cos(0) |
1.0 |
math.tan(x) |
Tangent function | math.tan(math.pi/4) |
1.0 |
math.pi |
Value of Pi | math.pi |
3.1415926535 |
math.e |
Value of Euler’s number (e) | math.e |
2.7182818284 |
🔹 Square root (math.sqrt())
import math
print(math.sqrt(25)) # 5.0🔹 Ceil and Floor (math.ceil(), math.floor())
print(math.ceil(4.2)) # 5
print(math.floor(4.8)) # 4🔹 Factorial (math.factorial())
print(math.factorial(5)) # 120 (5! = 5 × 4 × 3 × 2 × 1)🔹 Logarithm (math.log() and math.log10())
print(math.log(10)) # 2.302 (natural log)
print(math.log10(1000)) # 3.0 (log base 10)🔹 Trigonometric functions (math.sin(), math.cos(), math.tan())
print(math.sin(math.pi/2)) # 1.0
print(math.cos(0)) # 1.0
print(math.tan(math.pi/4)) # 1.0🔹 Pi and Euler’s number (math.pi, math.e)
print(math.pi) # 3.1415926535
print(math.e) # 2.7182818284❌ Forgetting to Import the math Module
print(math.sqrt(25)) # ❌ ERROR! NameError: name 'math' is not defined✅ Solution: Always import math first
import math
print(math.sqrt(25)) # ✅ Works correctly!❌ Using Negative Numbers in math.sqrt()
print(math.sqrt(-4)) # ❌ ERROR! ValueError: math domain error✅ Solution: Use cmath.sqrt() for complex numbers
import cmath
print(cmath.sqrt(-4)) # ✅ 2j (complex number)❌ Using Logarithm with Zero or Negative Numbers
print(math.log(0)) # ❌ ERROR! ValueError: math domain error✅ Solution: Use a positive number
print(math.log(1)) # ✅ Works correctly! (log(1) = 0)✔️ Python has built-in math functions like abs(), round(), max(), min(), and pow().
✔️ The math module provides advanced functions like math.sqrt(), math.factorial(), and math.log().
✔️ The math module also has trigonometric functions (sin(), cos(), tan()) and constants (pi, e).
✔️ Common mistakes include forgetting to import math and using invalid inputs for sqrt() or log().