Python From Beginner to Advanced

0% completed

Previous
Next
Python - math

Python's math module is a comprehensive library that provides a wide range of mathematical operations. These functions are designed to provide quick and efficient computations for trigonometry, logarithms, statistical operations, and more. It also includes constants such as π (pi) and e (Euler's number) that are commonly used in various mathematical calculations. This lesson will explore the various functionalities of the math module divided into specific sections, each covering different types of functions.

Theoretic Functions

This section of the math module includes functions that perform theoretical calculations and numeric representations. These functions help in rounding numbers, calculating mathematical constants, and performing algebraic computations among other operations.

Sr.NoFunctionDescription
1math.ceil(x)Returns the smallest integer greater than or equal to x.
2math.comb(n, k)Calculates the number of combinations to choose k items from n items without repetition and ordering.
3math.copysign(x, y)Returns x with the sign of y.
5math.fabs(x)Returns the absolute value of x.
6math.factorial(n)Calculates the factorial of n.
7math.floor(x)Returns the largest integer less than or equal to x.
8math.fmod(x, y)Returns the remainder of x divided by y using platform fmod function.
9math.frexp(x)Breaks x into its binary significand and an exponent of two.
10math.fsum(iterable)Calculates an accurate floating point sum of values in the iterable.
11math.gcd(*integers)Calculates the greatest common divisor of the specified integers.
16math.isqrt(n)Returns the integer square root of n.
17math.lcm(*integers)Calculates the least common multiple of the specified integers.
18math.ldexp(x, i)Computes x * 2**i.
19math.modf(x)Returns the fractional and integer parts of x.
20math.perm(n, k)Calculates the number of permutations to choose k items from n items with order and without repetition.
21math.prod(iterable, *, start=1)Calculates the product of all the elements in the input iterable.
22math.remainder(x, y)Computes the remainder of x with respect to y using the round-to-nearest method.
23math.trunc(x)Truncates x to the integer towards zero.
24math.ulp(x)Returns the unit in the last place of x.

Example: Using math.factorial and math.comb

In this example, we will calculate the factorial of a number and the number of combinations of choosing 2 items from 5.

Python3
Python3

. . . .

Explanation:

  • math.factorial(7): Calculates the factorial of 7, which is 7 * 6 * 5 * 4 * 3 * 2 * 1.
  • math.comb(5, 2): Calculates the number of ways to choose 2 items from a set of 5 without regard to the order.

Power and Logarithmic Functions

The math module also provides a variety of functions for performing power and logarithmic calculations, essential for scientific computing where exponential growth or decay patterns are analyzed. These functions include operations for exponential calculations, logarithms in various bases, and more.

Sr.NoFunctionDescription
1math.cbrt(x)Calculates the cube root of x.
2math.exp(x)Returns the exponential of x, (e^x).
3math.exp2(x)Returns (2^x), the base-2 exponential of x.
4math.expm1(x)Calculates (e^x - 1), providing precision for very small values of x.
5math.log(x)Computes the natural logarithm (base e) of x.
6math.log1p(x)Returns the natural logarithm of (1 + x), enhancing precision for small x.
7math.log2(x)Calculates the logarithm base 2 of x, often more accurate than using math.log(x, 2).
8math.log10(x)Computes the base-10 logarithm of x.
9math.pow(x, y)Equivalent to the expression (x^y), calculates the power of x raised to y.
10math.sqrt(x)Returns the square root of x.

Example: Using math.exp and math.log

In this example, we will calculate the exponential of a number and take the natural logarithm of a number.

Python3
Python3

. . . .

Explanation:

  • math.exp(1): Calculates (e^1), which is the mathematical constant e.
  • math.log(exp_value): Since exp_value is e, math.log(exp_value) returns the natural logarithm of e, which is 1.

This simple example demonstrates the use of exponential functions and their inverses, logarithmic functions, to perform precise and meaningful mathematical calculations.

Trigonometric Functions

The trigonometric functions in Python's math module are crucial for calculations involving angles, circles, and periodic functions. These are particularly useful in fields such as physics, engineering, and computer graphics.

Sr.NoFunctionDescription
1math.acos(x)Returns the arc cosine of x, in radians.
2math.asin(x)Returns the arc sine of x, in radians.
3math.atan(x)Returns the arc tangent of x, in radians.
4math.atan2(y, x)Returns atan(y / x), in radians. The function considers the sign of both arguments to determine the quadrant.
5math.cos(x)Returns the cosine of x radians.
6math.sin(x)Returns the sine of x radians.
7math.tan(x)Returns the tangent of x radians.
8math.hypot(x, y)Returns the Euclidean norm, (\sqrt{x^2 + y^2}). This is the length of the vector from the origin to point (x, y).

Example: Using math.sin and math.cos

In this example, we will calculate the sine and cosine of 45 degrees (converted to radians).

Python3
Python3

. . . .

Explanation:

  • math.radians(45): Converts 45 degrees into radians.
  • math.sin(angle_rad) and math.cos(angle_rad): Calculates the sine and cosine of the angle, demonstrating the trigonometric functions' role in converting angular measurements into ratios.

Angular Conversion Functions

Angular conversion functions in Python's math module facilitate the conversion between degrees and radians, which is essential when working with trigonometric functions that require angles in radians.

Sr.NoFunctionDescription
1math.degrees(x)Converts angle x from radians to degrees.
2math.radians(x)Converts angle x from degrees to radians.

Example: Converting Angles from Degrees to Radians and Back

In this example, we will convert an angle from degrees to radians, use it to calculate a trigonometric function, and then convert it back to degrees.

Python3
Python3

. . . .

Explanation:

  • math.radians(angle_deg): Converts the angle from degrees to radians for use in trigonometric calculations.
  • math.cos(angle_rad): Computes the cosine of the angle, now in radians.
  • math.degrees(angle_rad): Converts the angle back from radians to degrees to verify the conversion.

Mathematical Constants

The math module provides several mathematical constants that are frequently used in various calculations, including pi (π), Euler's number (e), and others.

Sr.NoConstantDescription
1math.piRepresents the mathematical constant pi (π), approximately 3.14159.
2math.eRepresents Euler's number, approximately 2.71828.
3math.tauRepresents the constant Tau (τ), which is 2π, approximately 6.28318.
4math.infRepresents positive infinity. For negative infinity, use -math.inf.
5math.nanRepresents a floating-point "Not a Number" (NaN) value.

Hyperbolic Functions

Hyperbolic functions, which are analogs of trigonometric functions based on hyperbolas instead of circles, are essential in various fields of science and engineering, particularly when dealing with exponential growth or decay.

Sr.NoFunctionDescription
1math.acosh(x)Returns the inverse hyperbolic cosine of x.
2math.asinh(x)Returns the inverse hyperbolic sine of x.
3math.atanh(x)Returns the inverse hyperbolic tangent of x, for
4math.cosh(x)Returns the hyperbolic cosine of x.
5math.sinh(x)Returns the hyperbolic sine of x.
6math.tanh(x)Returns the hyperbolic tangent of x.

Example: Using math.cosh and math.sinh

In this example, we will calculate the hyperbolic cosine and sine of a value.

Python3
Python3

. . . .

Explanation:

  • math.cosh(1) and math.sinh(1): Calculate the hyperbolic cosine and sine of 1. These functions are useful for understanding relationships in hyperbolic geometry and in solutions to various differential equations.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next