Python math functions

a = 12
b = 5

print(a + b)       # 17 , sum of two numbers
print(a - b)       # 7 , subtraction of two numbers
print(a * b)       # 60, product of two numbers
print(a / b)       # 2.4, quotient of two numbers
print(a // b)      # 2 , floored quotient   
print(a % b)       # 2, remainder of division
print(a ** b)      # 248832, a to the power b
print(pow(a, b))  # 248832, a to the power b
print(divmod(a, b)) # (2,2), the pair (a // b, a % b)

Importing Math library

You may get error saying about the missing math library need to be included before using these functions.
NameError: name 'math' is not defined
Just add this line before using the functions
import math

Why Import the math Module?

The math module in Python provides access to mathematical functions that are not built into the basic operators. Functions like square root, logarithm, and trigonometric calculations require the math module.

Example: Calculating Square Root

import math

num = 25

# Without math module (will cause an error)
# print(num ** 0.5)  # Works but not always accurate

# Using math module
print(math.sqrt(num))  # Output: 5.0

Explanation:

  • Without the math module: We can calculate square roots using **0.5, but it may not be precise for large or complex numbers.
  • Using math.sqrt(): This function ensures better accuracy and handles special cases correctly.

Thus, for precise mathematical calculations, we should import the math module in Python.

Math methods

bitwise operatorsUsing bitwise operators
floor & ceilGet lower or higher integer from the input number
fabsabsolute value of input number
factorial!n is the factorial of input number n
copysignCopy sign of one number to other
fmodModule of two input numbers
frexpGetting mantissa and exponent of input number
ldexpGetting the number by using mantissa and exponent
fsum()Sum of elements of iterable object
isfinite()checking the number is finite or not
isinf()checking the number is infinity or not
isnan()checking the number is NaN or not
modf()Separating fraction and integer parts
remainder()remainder of one number with respect to other
round()Rounding number to nearest float or Integer
trunc()Integer part of a number
exp()e raised to the power x
expm1()e raised to the power x -1
pow()x to raised to the power of y
sqrt()Returns square root of a number
logarithm
log(x[,base])Log of x using input base ( optional )
log1p(x)Log of 1+x using base e
log2(x)Log of x using base 2
log10(x)Log of x using base 10
Trigonometric functions
acos(x)arc cosine of x ( input in radian )
asin(x)arc sin of x ( input in radian )
atan(x)arc tan of x ( input in radian )
cos(x)cos of x ( input in radian )
sin(x)sin of x ( input in radian )
tan(x)tan of x ( input in radian )
acosh(x)inverse hyperbolic cosine of x
asinh(x)inverse hyperbolic sine of x
atanh(x)inverse hyperbolic tangent of x
cosh(x) hyperbolic cosine of x
sinh(x) hyperbolic sine of x
tanh(x) hyperbolic tangent of x

Mathematical Constants in Python

Python's math module provides several essential mathematical constants that help in calculations involving geometry, logarithms, and infinite values.

  • math.pi π = 3.141592653589793 — Represents the mathematical constant Pi, used in trigonometry and geometry.
  • math.e e = 2.718281828459045 — The base of natural logarithms, commonly used in exponential and logarithmic functions.
  • math.tau τ = 6.283185307179586 — Represents the ratio of a circle's circumference to its radius, equivalent to .
  • math.inf — Represents floating-point positive infinity, used to define unbounded values in calculations.
  • -math.inf — Represents floating-point negative infinity.
  • math.nan — Stands for "Not a Number," used in cases where a mathematical operation does not result in a valid number (e.g., 0/0).

These constants are useful in various computations, including scientific calculations, probability models, and numerical simulations.

Examples using Math module

Standard DeviationStandar Deviation for a list of numbers
Random NumberGenerate Random integer and float

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer