Python Math

Python provides a wide range of mathematical functions and operations to handle calculations effectively. The built-in math module is the foundation for performing advanced mathematical operations. It contains various methods for arithmetic calculations, trigonometry, logarithms, factorials and much more.

Why Learn Python Math?

  1. Efficient Computation: Perform complex calculations quickly.
  2. Scientific Applications: Useful for data analysis, AI, ML and scientific computing.
  3. Everyday Applications: Essential for financial modeling, game development and engineering tasks.

Getting Started with the Math Module

To use the math module, you need to import it first.

Example: Importing the math Module

import math

The math module provides several constants and functions for mathematical operations.

Mathematical Constants

Python’s math module includes two essential constants:

  1. math.pi: Represents the value of π (approximately 3.14159).
  2. math.e: Represents the value of Euler’s number (approximately 2.71828).

Example: Using Mathematical Constants

import math

print("Value of Pi:", math.pi)
print("Value of Euler's Number (e):", math.e)

Output:

Value of Pi: 3.141592653589793  
Value of Euler's Number (e): 2.718281828459045

Common Mathematical Functions

1. Rounding Functions

  • math.ceil(x): Rounds up to the nearest integer.
  • math.floor(x): Rounds down to the nearest integer.

Example: Ceiling and Floor

import math

print("Ceil of 3.4:", math.ceil(3.4))
print("Floor of 3.4:", math.floor(3.4))

Output:

Ceil of 3.4: 4  
Floor of 3.4: 3

2. Power and Square Root

  • math.pow(x, y): Returns x raised to the power y.
  • math.sqrt(x): Returns the square root of x.

Example: Power and Square Root

import math

print("3 raised to the power 4:", math.pow(3, 4))
print("Square root of 16:", math.sqrt(16))

Output:

3 raised to the power 4: 81.0  
Square root of 16: 4.0

3. Trigonometric Functions

Python’s math module includes trigonometric functions like sine, cosine and tangent.

  • math.sin(x): Sine of x (in radians).
  • math.cos(x): Cosine of x (in radians).
  • math.tan(x): Tangent of x (in radians).

Example: Trigonometry

import math

angle = math.pi / 6 # 30 degrees in radians
print("Sine of 30°:", math.sin(angle))
print("Cosine of 30°:", math.cos(angle))
print("Tangent of 30°:", math.tan(angle))

Output:

Sine of 30°: 0.49999999999999994  
Cosine of 30°: 0.8660254037844387
Tangent of 30°: 0.5773502691896257

4. Logarithmic Functions

  • math.log(x): Returns the natural logarithm (base e) of x.
  • math.log10(x): Returns the base-10 logarithm of x.

Example: Logarithms

import math

print("Natural log of 10:", math.log(10))
print("Base-10 log of 10:", math.log10(10))

Output:

Natural log of 10: 2.302585092994046  
Base-10 log of 10: 1.0

5. Factorial

  • math.factorial(x): Returns the factorial of x.

Example: Factorial

import math

print("Factorial of 5:", math.factorial(5))

Output:

Factorial of 5: 120

6. Absolute Value

  • math.fabs(x): Returns the absolute value of x as a float.

Example: Absolute Value

import math

print("Absolute value of -7.5:", math.fabs(-7.5))

Output:

Absolute value of -7.5: 7.5

Advanced Mathematical Functions

1. Exponential

  • math.exp(x): Returns e raised to the power x.

Example: Exponential

import math

print("e raised to the power 3:", math.exp(3))

Output:

e raised to the power 3: 20.085536923187668

2. Greatest Common Divisor (GCD)

  • math.gcd(x, y): Returns the greatest common divisor of x and y.

Example: GCD

import math

print("GCD of 12 and 18:", math.gcd(12, 18))

Output:

GCD of 12 and 18: 6

3. Degrees and Radians

  • math.radians(x): Converts degrees to radians.
  • math.degrees(x): Converts radians to degrees.

Example: Degrees and Radians

import math

print("45 degrees in radians:", math.radians(45))
print("π/4 radians in degrees:", math.degrees(math.pi / 4))

Output:

45 degrees in radians: 0.7853981633974483  
π/4 radians in degrees: 45.0

Using Math Functions in Real-World Scenarios

1. Calculating the Area of a Circle

import math

radius = 5
area = math.pi * math.pow(radius, 2)
print("Area of the circle:", area)

Output:

Area of the circle: 78.53981633974483

2. Distance Between Two Points

import math

x1, y1 = 3, 4
x2, y2 = 7, 1
distance = math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))
print("Distance between points:", distance)

Output:

Distance between points: 5.0

Leave a Comment