Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Mathematical Functions in Python?
Python's math module provides essential mathematical functions for performing operations ranging from basic arithmetic to complex trigonometric and logarithmic calculations. All math functions work with integers and real numbers, but not with complex numbers.
To use mathematical functions, you need to import the math module ?
import math
Mathematical Constants
The math module provides several important mathematical constants ?
| Constant | Description |
|---|---|
math.pi |
Returns the value of ?: 3.141592 |
math.e |
Returns the value of natural base e: 2.718282 |
math.tau |
Returns the value of ? (2?): 6.283185 |
math.inf |
Returns positive infinity |
math.nan |
Represents "Not a Number" |
Number Representation Functions
These functions help manipulate and analyze numeric values ?
| Function | Description |
|---|---|
ceil(x) |
Returns the smallest integer greater than or equal to x |
floor(x) |
Returns the largest integer less than or equal to x |
fabs(x) |
Returns the absolute value of x |
factorial(x) |
Returns factorial of x (where x ? 0) |
gcd(x, y) |
Returns the greatest common divisor of x and y |
isnan(x) |
Checks if x is "Not a Number" |
isinf(x) |
Checks if x is infinity |
Example
import math
# Floor and Ceiling
print('The Floor and Ceiling value of 9.45 are:',
math.floor(9.45), ',', math.ceil(9.45))
# Absolute value and factorial
print('Absolute value of -94:', math.fabs(-94))
print('Factorial of 5:', math.factorial(5))
# GCD and sum
print('GCD of 24 and 48:', math.gcd(24, 48))
# Check for special values
x = float('nan')
y = float('inf')
print('Is x NaN?', math.isnan(x))
print('Is y infinity?', math.isinf(y))
The Floor and Ceiling value of 9.45 are: 9 , 10 Absolute value of -94: 94.0 Factorial of 5: 120 GCD of 24 and 48: 24 Is x NaN? True Is y infinity? True
Power and Logarithmic Functions
These functions handle exponential and logarithmic calculations ?
| Function | Description |
|---|---|
pow(x, y) |
Returns x raised to the power y |
sqrt(x) |
Returns the square root of x |
exp(x) |
Returns ex |
log(x, base) |
Returns logarithm of x to given base (default: e) |
log10(x) |
Returns base-10 logarithm of x |
log2(x) |
Returns base-2 logarithm of x |
Example
import math
print("2^5 =", math.pow(2, 5))
print("Square root of 625:", math.sqrt(625))
print("e^2 =", math.exp(2))
print("log(100) base 10:", math.log10(100))
print("log(8) base 2:", math.log2(8))
print("log(25) base 5:", math.log(25, 5))
2^5 = 32.0 Square root of 625: 25.0 e^2 = 7.38905609893065 log(100) base 10: 2.0 log(8) base 2: 3.0 log(25) base 5: 2.0
Trigonometric Functions
These functions perform trigonometric operations. Input angles should be in radians ?
| Function | Description |
|---|---|
sin(x) |
Returns sine of x (in radians) |
cos(x) |
Returns cosine of x (in radians) |
tan(x) |
Returns tangent of x (in radians) |
asin(x) |
Returns inverse sine of x |
radians(x) |
Converts degrees to radians |
degrees(x) |
Converts radians to degrees |
Example
import math
# Convert 45 degrees to radians and find sine
print("sin(45°):", math.sin(math.radians(45)))
print("cos(60°):", math.cos(math.radians(60)))
print("tan(45°):", math.tan(math.radians(45)))
# Inverse trigonometric functions
print("arcsin(0.5) in degrees:", math.degrees(math.asin(0.5)))
# Using pi constant
print("cos(?):", math.cos(math.pi))
sin(45°): 0.7071067811865475 cos(60°): 0.5000000000000001 tan(45°): 0.9999999999999999 arcsin(0.5) in degrees: 30.000000000000004 cos(?): -1.0
Conclusion
Python's math module provides comprehensive mathematical functionality for scientific computing and data analysis. Use math.radians() when working with trigonometric functions, and remember that logarithmic functions require positive arguments.
