Python Math

Python provides a wide range of built-in mathematical functions and operators. Additionally, the math module offers many useful mathematical functions beyond the basic operations.


Basic Mathematical Operations

Python supports basic arithmetic operations such as addition, subtraction, multiplication, and division.

Explanation:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
# Basic arithmetic operations
a = 10
b = 5

print("Addition:", a + b)        # Output: 15
print("Subtraction:", a - b)     # Output: 5
print("Multiplication:", a * b)  # Output: 50
print("Division:", a / b)        # Output: 2.0




Using the math Module

The math module provides access to various mathematical functions and constants.

Explanation:

  • math.sqrt(x): Returns the square root of x.
  • math.pow(x, y): Returns x raised to the power of y.
  • math.pi: The mathematical constant π (pi).
  • math.e: The mathematical constant e (Euler’s number).
import math

# Using math module functions and constants
print("Square root of 16:", math.sqrt(16))       # Output: 4.0
print("2 raised to the power of 3:", math.pow(2, 3))  # Output: 8.0
print("Value of pi:", math.pi)                   # Output: 3.141592653589793
print("Value of e:", math.e)                     # Output: 2.718281828459045




Trigonometric Functions

The math module includes trigonometric functions for performing calculations involving angles.

Explanation:

  • math.sin(x): Returns the sine of x (in radians).
  • math.cos(x): Returns the cosine of x (in radians).
  • math.tan(x): Returns the tangent of x (in radians).
  • math.radians(x): Converts degrees to radians.
  • math.degrees(x): Converts radians to degrees.
import math

# Using trigonometric functions
angle_degrees = 45
angle_radians = math.radians(angle_degrees)

print("Sine of 45 degrees:", math.sin(angle_radians))  # Output: 0.7071067811865476
print("Cosine of 45 degrees:", math.cos(angle_radians))  # Output: 0.7071067811865476
print("Tangent of 45 degrees:", math.tan(angle_radians))  # Output: 1.0
print("45 degrees in radians:", angle_radians)           # Output: 0.7853981633974483
print("0.785 radians in degrees:", math.degrees(angle_radians))  # Output: 45.0




Logarithmic Functions

The math module also includes logarithmic functions for performing log calculations.

Explanation:

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

# Using logarithmic functions
print("Natural log of 10:", math.log(10))      # Output: 2.302585092994046
print("Base-10 log of 100:", math.log10(100))  # Output: 2.0




Python Math Example Code

Explanation:

  • Basic arithmetic operations: Demonstrates addition, subtraction, multiplication, and division.
  • Using the math module: Shows the use of functions and constants in the math module.
  • Trigonometric functions: Uses trigonometric functions for angle calculations.
  • Logarithmic functions: Performs logarithmic calculations.
import math

# Basic arithmetic operations
a = 10
b = 5

print("Addition:", a + b)        # Output: 15
print("Subtraction:", a - b)     # Output: 5
print("Multiplication:", a * b)  # Output: 50
print("Division:", a / b)        # Output: 2.0

# Using math module functions and constants
print("Square root of 16:", math.sqrt(16))       # Output: 4.0
print("2 raised to the power of 3:", math.pow(2, 3))  # Output: 8.0
print("Value of pi:", math.pi)                   # Output: 3.141592653589793
print("Value of e:", math.e)                     # Output: 2.718281828459045

# Using trigonometric functions
angle_degrees = 45
angle_radians = math.radians(angle_degrees)

print("Sine of 45 degrees:", math.sin(angle_radians))  # Output: 0.7071067811865476
print("Cosine of 45 degrees:", math.cos(angle_radians))  # Output: 0.7071067811865476
print("Tangent of 45 degrees:", math.tan(angle_radians))  # Output: 1.0
print("45 degrees in radians:", angle_radians)           # Output: 0.7853981633974483
print("0.785 radians in degrees:", math.degrees(angle_radians))  # Output: 45.0

# Using logarithmic functions
print("Natural log of 10:", math.log(10))      # Output: 2.302585092994046
print("Base-10 log of 100:", math.log10(100))  # Output: 2.0




Python Labs

Scroll to Top