Welcome to ! In this tutorial, we will explore the math module in Python. We'll cover how to perform various mathematical operations, work with constants, and provide detailed examples to illustrate their application.
Table of Contents
Introduction to the math Module
Common Mathematical Functions
Trigonometric Functions
Logarithmic and Exponential Functions
Special Functions
Mathematical Constants
Practical Examples
Summary
1. Introduction to the math Module
What is the math Module?
The math module in Python provides access to various mathematical functions and constants. It includes functions for arithmetic operations, trigonometry, logarithms, and more.
Key Points
The math module contains a wide range of mathematical functions.
It provides access to mathematical constants like pi and e.
Example: Using Logarithmic and Exponential Functions
import math
# Natural logarithm
print(math.log(10)) # Output: 2.302585092994046
# Logarithm base 10
print(math.log10(10)) # Output: 1.0
# Exponential function
print(math.exp(2)) # Output: 7.38905609893065
# Power function (equivalent to ** operator)
print(math.pow(2, 3)) # Output: 8.0
5. Special Functions
Example: Using Special Functions
import math
# Gamma function
print(math.gamma(5)) # Output: 24.0
# Error function
print(math.erf(1)) # Output: 0.8427007929497149
# Complementary error function
print(math.erfc(1)) # Output: 0.15729920705028513
6. Mathematical Constants
Example: Using Mathematical Constants
import math
# Pi
print(math.pi) # Output: 3.141592653589793
# Euler's number (e)
print(math.e) # Output: 2.718281828459045
# Tau (2*pi)
print(math.tau) # Output: 6.283185307179586
# Infinity
print(math.inf) # Output: inf
# Not a Number (NaN)
print(math.nan) # Output: nan
7. Practical Examples
Example 1: Calculating the Area of a Circle
import math
def area_of_circle(radius):
return math.pi * math.pow(radius, 2)
radius = 5
print(f"Area of circle with radius {radius}: {area_of_circle(radius)}")
Example 2: Calculating Compound Interest
import math
def compound_interest(principal, rate, time):
return principal * math.pow((1 + rate / 100), time)
principal = 1000
rate = 5
time = 2
print(f"Compound Interest: {compound_interest(principal, rate, time)}")
Example 3: Solving a Quadratic Equation
import math
def solve_quadratic(a, b, c):
discriminant = math.pow(b, 2) - 4 * a * c
if discriminant < 0:
return "No real roots"
elif discriminant == 0:
root = -b / (2 * a)
return f"One root: {root}"
else:
root1 = (-b + math.sqrt(discriminant)) / (2 * a)
root2 = (-b - math.sqrt(discriminant)) / (2 * a)
return f"Two roots: {root1} and {root2}"
a, b, c = 1, -3, 2
print(solve_quadratic(a, b, c))
8. Summary
In this tutorial, we explored the math module in Python, its importance, and how to use its functions and constants. We covered common mathematical functions, trigonometric functions, logarithmic and exponential functions, special functions, and mathematical constants. We also provided practical examples to illustrate the application of the math module. The math module is a powerful tool for performing mathematical operations in Python.
For more tutorials and in-depth explanations, visit !
This tutorial provides a comprehensive overview of Python's math module, detailing each topic and subtopic with examples and explanations. For more such tutorials, keep following !