You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

Python's math is a powerful tool for mathematical operations. It offers a wide range of functions and constants beyond basic arithmetic, enabling complex calculations in scientific and engineering applications.

From trigonometry to logarithms, the math module enhances Python's mathematical capabilities. It provides precise constants like pi and e, and functions for advanced operations, making it essential for solving numerical problems efficiently.

Math Module

Built-in vs math module functions

Top images from around the web for Built-in vs math module functions
Top images from around the web for Built-in vs math module functions

Built-in functions readily available in Python without importing any modules (

abs()
,
round()
,
min()
,
max()
,
pow()
). Math module functions require importing the math module:
import math
provide additional mathematical functions and constants (
[math.sqrt()](https://www.fiveableKeyTerm:math.sqrt())
,
[math.sin()](https://www.fiveableKeyTerm:math.sin())
,
[math.log()](https://www.fiveableKeyTerm:math.log())
,
[math.pi](https://www.fiveableKeyTerm:math.pi)
,
[math.e](https://www.fiveableKeyTerm:math.e)
). Use math module functions when dealing with (
sin()
,
cos()
,
tan()
), performing logarithmic or exponential calculations (
log()
,
exp()
), or requiring like pi (π\pi) or e. Use built-in functions for basic arithmetic and rounding operations (such as
round()
,
ceil()
, and
floor()
).

Math module for numerical problems

Constants in the math module include

math.pi
, the mathematical constant pi (π3.141592653589793\pi \approx 3.141592653589793), and
math.e
, the mathematical constant e (Euler's number, e2.718281828459045e \approx 2.718281828459045). The math module provides various mathematical constants for precise calculations. Functions in the math module:
math.sqrt(x)
returns the square root of x
math.pow(x, y)
returns x raised to the power of y
math.exp(x)
returns e raised to the power of x
math.log(x[, base])
returns the logarithm of x to the given base (default is natural logarithm, base e) Trigonometric functions:
math.sin(x)
,
math.cos(x)
,
math.tan(x)
accept angles in radians Inverse trigonometric functions:
math.asin(x)
,
math.acos(x)
,
math.atan(x)
return angles in radians

Example usage:

import math

# Calculate the area of a circle with radius 5
area = math.pi * math.pow(5, 2)
print(area)  # Output: 78.53981633974483

# Calculate the logarithm of 100 with base 10
log_value = math.log(100, 10)
print(log_value)  # Output: 2.0

Degree-radian conversion with math module

Angle units: degrees commonly used in everyday life (0° to 360°) and radians used in mathematical calculations (0 to 2π\pi). Converting between degrees and radians:

math.radians(x)
converts angle x from degrees to radians
math.degrees(x)
converts angle x from radians to degrees

Conversion formulas: Degrees to radians: radians=degrees×π180radians = degrees \times \frac{\pi}{180} Radians to degrees: degrees=radians×180πdegrees = radians \times \frac{180}{\pi}

Example usage:

import math

# Convert 45 degrees to radians
angle_rad = math.radians(45)
print(angle_rad)  # Output: 0.7853981633974483

# Convert pi/4 radians to degrees
angle_deg = math.degrees(math.pi/4)
print(angle_deg)  # Output: 45.0

Advanced Mathematical Functions

The math module provides a range of functions for more complex mathematical operations:

  • :
    math.log()
    ,
    math.log10()
    ,
    math.log2()
    for natural, base-10, and base-2 logarithms
  • :
    [math.exp()](https://www.fiveableKeyTerm:math.exp())
    ,
    math.expm1()
    for e^x and e^x - 1
  • Trigonometric functions:
    math.sin()
    ,
    [math.cos()](https://www.fiveableKeyTerm:math.cos())
    ,
    [math.tan()](https://www.fiveableKeyTerm:math.tan())
    , and their inverse functions
  • Hyperbolic functions:
    math.sinh()
    ,
    math.cosh()
    ,
    math.tanh()
    , and their inverse functions These functions are essential for various applications in scientific computing and .
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary