Math

Python offers a variety of built-in mathematical functions and an extensive math module to handle various mathematical operations efficiently. This tutorial will guide you through some essential math functions and how to use the math module for more advanced calculations.

Built-in Math Functions

Python includes several built-in functions to perform basic mathematical operations:

Finding Minimum and Maximum Values

The min() and max() functions help you find the smallest or largest value within a set of numbers or an iterable.


x = min(5, 15, 25)
y = max(5, 15, 25)

print(x)  # Output: 5
print(y)  # Output: 25

Calculating Absolute Values

The abs() function returns the absolute (non-negative) value of a number.


x = abs(-7.25)

print(x)  # Output: 7.25

Exponentiation

Use the pow(x, y) function to compute x raised to the power of y.


x = pow(4, 3)

print(x)  # Output: 64

Using the Math Module

Python’s math module provides additional mathematical functions and constants. To use this module, you first need to import it:


import math

Here are some useful functions and constants from the math module:

Square Root Calculation

The math.sqrt() function returns the square root of a given number.


import math

x = math.sqrt(64)

print(x)  # Output: 8.0

Rounding Numbers

The math.ceil() method rounds a number up to the nearest integer, while the math.floor() method rounds a number down to the nearest integer.


import math

x = math.ceil(1.4)
y = math.floor(1.4)

print(x)  # Output: 2
print(y)  # Output: 1

Constant PI

The math.pi constant provides an approximation of the mathematical constant π (pi).


import math

x = math.pi

print(x)  # Output: 3.141592653589793

By leveraging these built-in functions and the math module, you can perform a wide range of mathematical operations efficiently in Python. Explore these tools to enhance your calculations and data processing.

Python Math Methods and Constants

Math Methods

MethodDescription
math.acos()Returns the arc cosine of a number.
math.acosh()Returns the inverse hyperbolic cosine of a number.
math.asin()Returns the arc sine of a number.
math.asinh()Returns the inverse hyperbolic sine of a number.
math.atan()Returns the arc tangent of a number in radians.
math.atan2()Returns the arc tangent of y/x in radians.
math.atanh()Returns the inverse hyperbolic tangent of a number.
math.ceil()Rounds a number up to the nearest integer.
math.comb()Returns the number of ways to choose k items from n items without repetition and order.
math.copysign()Returns a float with the magnitude of the first parameter and the sign of the second parameter.
math.cos()Returns the cosine of a number.
math.cosh()Returns the hyperbolic cosine of a number.
math.degrees()Converts an angle from radians to degrees.
math.dist()Returns the Euclidean distance between two points (p and q), where p and q are the coordinates of those points.
math.erf()Returns the error function of a number.
math.erfc()Returns the complementary error function of a number.
math.exp()Returns E raised to the power of x.
math.expm1()Returns Ex – 1.
math.fabs()Returns the absolute value of a number.
math.factorial()Returns the factorial of a number.
math.floor()Rounds a number down to the nearest integer.
math.fmod()Returns the remainder of x/y.
math.frexp()Returns the mantissa and the exponent of a specified number.
math.fsum()Returns the sum of all items in any iterable (tuples, arrays, lists, etc.).
math.gamma()Returns the gamma function at x.
math.gcd()Returns the greatest common divisor of two integers.
math.hypot()Returns the Euclidean norm.
math.isclose()Checks whether two values are close to each other or not.
math.isfinite()Checks whether a number is finite or not.
math.isinf()Checks whether a number is infinite or not.
math.isnan()Checks whether a value is NaN (not a number) or not.
math.isqrt()Rounds a square root number downwards to the nearest integer.
math.ldexp()Returns the inverse of math.frexp() which is x * (2**i) of the given numbers x and i.
math.lgamma()Returns the log gamma value of x.
math.log()Returns the natural logarithm (base e) of a number, or the logarithm of number to base.
math.log10()Returns the base-10 logarithm of a number.
math.log1p()Returns the natural logarithm of 1+x.
math.log2()Returns the base-2 logarithm of x.

Math Constants

ConstantDescription
math.eReturns Euler’s number (approximately 2.71828).
math.infReturns a floating-point positive infinity.
math.nanReturns a floating-point NaN (Not a Number) value.
math.piReturns the mathematical constant pi (approximately 3.14159).
math.tauReturns the mathematical constant tau (approximately 6.28318).

Leave a Comment