Python Math

The math module in Python provides functions for trigonometry, logarithms, powers, statistics, and constants.

1. Importing the math Module

The math module is a standard library module, so no additional installation is required. Import it with:
import math

2. Basic Mathematical Constants

The math module provides common constants, like pi and e.
import math

# Constants
print("Pi:", math.pi)
print("Euler's number:", math.e)

Output:

Pi: 3.141592653589793
Euler's number: 2.718281828459045
Explanation: math.pi and math.e are constants representing π and e.

3. Rounding Functions

The math module includes several functions for rounding numbers.
# Floor, ceil, and trunc functions
print("Floor of 3.7:", math.floor(3.7))
print("Ceil of 3.2:", math.ceil(3.2))
print("Truncate 3.9:", math.trunc(3.9))

Output:

Floor of 3.7: 3
Ceil of 3.2: 4
Truncate 3.9: 3
Explanation: math.floor() returns the largest integer less than or equal to a number, math.ceil() returns the smallest integer greater than or equal to a number, and math.trunc() removes the decimal part.

4. Power and Logarithmic Functions

The math module provides power and logarithmic functions, including math.pow(), math.sqrt(), and math.log().
# Power, square root, and logarithm
print("2 raised to the power 3:", math.pow(2, 3))
print("Square root of 16:", math.sqrt(16))
print("Natural logarithm of 10:", math.log(10))
print("Logarithm base 10 of 100:", math.log10(100))

Output:

2 raised to the power 3: 8.0
Square root of 16: 4.0
Natural logarithm of 10: 2.302585092994046
Logarithm base 10 of 100: 2.0
Explanation: math.pow() raises a number to a specified power, math.sqrt() finds the square root, and math.log() calculates the natural logarithm. math.log10() calculates the base-10 logarithm.

5. Trigonometric Functions

The math module provides trigonometric functions such as sine, cosine, and tangent.
# Trigonometric functions
print("Sine of pi/2:", math.sin(math.pi / 2))
print("Cosine of pi:", math.cos(math.pi))
print("Tangent of pi/4:", math.tan(math.pi / 4))

Output:

Sine of pi/2: 1.0
Cosine of pi: -1.0
Tangent of pi/4: 0.9999999999999999
Explanation: The functions math.sin(), math.cos(), and math.tan() calculate the sine, cosine, and tangent of an angle in radians.

6. Angle Conversion Functions

Convert between radians and degrees using math.radians() and math.degrees().
# Convert degrees to radians and vice versa
print("Radians of 180 degrees:", math.radians(180))
print("Degrees of pi radians:", math.degrees(math.pi))

Output:

Radians of 180 degrees: 3.141592653589793
Degrees of pi radians: 180.0
Explanation: math.radians() converts degrees to radians, while math.degrees() converts radians to degrees.

7. Hyperbolic Functions

The math module also provides hyperbolic functions like math.sinh(), math.cosh(), and math.tanh().
# Hyperbolic functions
print("Sinh of 1:", math.sinh(1))
print("Cosh of 0:", math.cosh(0))
print("Tanh of 1:", math.tanh(1))

Output:

Sinh of 1: 1.1752011936438014
Cosh of 0: 1.0
Tanh of 1: 0.7615941559557649
Explanation: Hyperbolic functions, such as math.sinh(), math.cosh(), and math.tanh(), are similar to trigonometric functions but for hyperbolic angles.

8. Special Functions

The math module includes functions for factorials and gamma calculations.
# Factorial and gamma functions
print("Factorial of 5:", math.factorial(5))
print("Gamma function at 5:", math.gamma(5))

Output:

Factorial of 5: 120
Gamma function at 5: 24.0
Explanation: math.factorial() returns the factorial of an integer, and math.gamma() computes the gamma function, useful in complex mathematical contexts.

9. Floating-Point Functions

The math module includes functions for handling floating-point numbers with precision, such as math.isclose(), math.fabs(), and math.copysign().
# Floating-point functions
print("Absolute value of -3.7:", math.fabs(-3.7))
print("Copy sign from -2 to 3.5:", math.copysign(3.5, -2))
print("Are 0.1 and 0.1000000001 close?:", math.isclose(0.1, 0.1000000001))

Output:

Absolute value of -3.7: 3.7
Copy sign from -2 to 3.5: -3.5
Are 0.1 and 0.1000000001 close?: True
Explanation: math.fabs() returns the absolute value of a floating-point number, math.copysign() assigns the sign of one number to another, and math.isclose() checks if two numbers are nearly equal.

Summary

The Python math module provides a wide range of mathematical functions for scientific calculations, including constants, trigonometric functions, hyperbolic functions, power functions, and more.

Previous: Python XML Processing | Next: Python REST

<
>