Course Content
What is Python?
Introduction of Python and Its setup
0/2
Control Statement
Control statements are used to control the flow of execution depending upon the specified condition/logic.
0/5
File Handling
File handling is an important component of any application. Python has multiple functions for creating, reading, updating, and deleting files.
0/2
Python
    About Lesson

    Python provides a rich set of tools for performing mathematical operations. The built-in math module offers a wide range of mathematical functions for tasks like finding square roots, logarithms, trigonometric calculations, and more.

     

    1. What is the Math Module?

    The math module in Python provides access to standard mathematical functions defined in C. To use these functions, you must import the math module.

    Importing the Math Module:

     

    import math

     

    2. Commonly Used Math Functions

    2.1 Basic Math Functions

    1. math.ceil(x): Rounds a number up to the nearest integer.
    2. math.floor(x): Rounds a number down to the nearest integer.
    3. math.fabs(x): Returns the absolute value of x.
    4. math.factorial(x): Returns the factorial of x (only for non-negative integers).
    5. math.fmod(x, y): Returns the remainder of x divided by y.

    Examples:

    import math

    print(math.ceil(4.3)) # Outputs: 5
    print(math.floor(4.7)) # Outputs: 4
    print(math.fabs(-10)) # Outputs: 10.0
    print(math.factorial(5)) # Outputs: 120
    print(math.fmod(10, 3)) # Outputs: 1.0

     

    2.2 Power and Logarithmic Functions

    1. math.sqrt(x): Returns the square root of x.
    2. math.pow(x, y): Returns x raised to the power of y (same as x ** y).
    3. math.log(x): Returns the natural logarithm of x.
    4. math.log10(x): Returns the base-10 logarithm of x.
    5. math.exp(x): Returns e raised to the power of x.

    Examples:

    print(math.sqrt(16)) # Outputs: 4.0
    print(math.pow(2, 3)) # Outputs: 8.0
    print(math.log(2.7183)) # Outputs: ~1.0
    print(math.log10(100)) # Outputs: 2.0
    print(math.exp(1)) # Outputs: ~2.718

     

    2.3 Trigonometric Functions

    Python provides trigonometric functions for angles in radians.

    1. math.sin(x): Sine of x.
    2. math.cos(x): Cosine of x.
    3. math.tan(x): Tangent of x.
    4. math.asin(x), math.acos(x), math.atan(x): Inverse sine, cosine, and tangent.
    5. math.degrees(x): Converts radians to degrees.
    6. math.radians(x): Converts degrees to radians.

    Examples:

    print(math.sin(math.radians(30))) # Outputs: 0.5
    print(math.cos(math.radians(60))) # Outputs: 0.5
    print(math.tan(math.radians(45))) # Outputs: 1.0

    print(math.degrees(math.pi)) # Outputs: 180.0
    print(math.radians(180)) # Outputs: ~3.14159

     

    2.4 Constants in Math Module

    Python’s math module also includes some important constants:

    1. math.pi: Represents the value of π (pi).
    2. math.e: Represents the base of the natural logarithm (e).
    3. math.tau: Represents 2π (tau).
    4. math.inf: Represents infinity.
    5. math.nan: Represents Not a Number (NaN).

    Examples:

    print(math.pi) # Outputs: 3.141592653589793
    print(math.e) # Outputs: 2.718281828459045
    print(math.tau) # Outputs: 6.283185307179586

     

    3. Advanced Math Functions

    3.1 Hyperbolic Functions

    1. math.sinh(x): Hyperbolic sine.
    2. math.cosh(x): Hyperbolic cosine.
    3. math.tanh(x): Hyperbolic tangent.

    Example:

    print(math.sinh(1)) # Outputs: 1.1752011936438014
    print(math.cosh(1)) # Outputs: 1.5430806348152437
    print(math.tanh(1)) # Outputs: 0.7615941559557649

     

    3.2 GCD and LCM

    1. math.gcd(x, y): Returns the greatest common divisor of x and y.
    2. math.lcm(x, y) (Python 3.9+): Returns the least common multiple of x and y.

    Example:

    print(math.gcd(12, 18)) # Outputs: 6
    print(math.lcm(12, 18)) # Outputs: 36

     

    3.3 Rounding and Precision

    1. math.trunc(x): Truncates the decimal part, returning the integer part of x.
    2. math.isclose(a, b, rel_tol): Checks if two values are close to each other within a relative tolerance.

    Example:

    print(math.trunc(4.8)) # Outputs: 4
    print(math.isclose(0.1 + 0.2, 0.3)) # Outputs: True

     

    3.4 Handling NaN and Infinity

    1. math.isnan(x): Checks if x is NaN.
    2. math.isinf(x): Checks if x is infinite.

    Example:

    print(math.isnan(float('nan'))) # Outputs: True
    print(math.isinf(float('inf'))) # Outputs: True

     

    4. Real-World Example: Area of a Circle

    Using the math module to calculate the area of a circle:

     

    import math

    def calculate_circle_area(radius):
    return math.pi * math.pow(radius, 2)

    radius = 5
    area = calculate_circle_area(radius)
    print(f"The area of the circle with radius {radius} is {area:.2f}")
    # Outputs: The area of the circle with radius 5 is 78.54

     

    5. Summary

    • Python’s math module provides built-in support for mathematical operations and constants.
    • It includes basic arithmetic, power functions, logarithms, trigonometry, and more.
    • Use the math module for precision and performance in mathematical computations.

    The math module is an essential tool for any Python developer working with numerical or scientific problems!