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.
+: 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.0Using the math Module
The math module provides access to various mathematical functions and constants.
math.sqrt(x): Returns the square root ofx.math.pow(x, y): Returnsxraised to the power ofy.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.718281828459045Trigonometric Functions
The math module includes trigonometric functions for performing calculations involving angles.
math.sin(x): Returns the sine ofx(in radians).math.cos(x): Returns the cosine ofx(in radians).math.tan(x): Returns the tangent ofx(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.0Logarithmic Functions
The math module also includes logarithmic functions for performing log calculations.
math.log(x): Returns the natural logarithm ofx.math.log10(x): Returns the base-10 logarithm ofx.
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.0Python Math Example Code
- Basic arithmetic operations: Demonstrates addition, subtraction, multiplication, and division.
- Using the
mathmodule: Shows the use of functions and constants in themathmodule. - 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


