Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Math Module in Python

Math Module in Python

The math Module in Python

The math module in Python provides a set of mathematical functions and constants that allow you to perform various advanced mathematical operations. The module includes functions for trigonometry, logarithms, factorials, constants like pi, and much more.

You need to import the math module to use its functions.

import math

Key Functions and Constants in the math Module

1. Constants

  • math.pi: The constant ? (pi), approximately 3.141592653589793.

  • math.e: The constant e (Euler's number), approximately 2.718281828459045.

  • math.tau: The constant ? (tau, 2 * ?), approximately 6.283185307179586.

  • math.inf: Positive infinity.

  • math.nan: Not a Number (NaN), used to represent undefined or unrepresentable values.

import mathprint(math.pi)   # Output: 3.141592653589793print(math.e)    # Output: 2.718281828459045print(math.inf)  # Output: inf

2. Trigonometric Functions

The trigonometric functions in the math module expect angles to be in radians, not degrees. You can convert degrees to radians using the math.radians() function.

  • math.sin(x): Sine of x (x in radians).

  • math.cos(x): Cosine of x (x in radians).

  • math.tan(x): Tangent of x (x in radians).

  • math.asin(x): Inverse sine (arcsin) of x, result in radians.

  • math.acos(x): Inverse cosine (arccos) of x, result in radians.

  • math.atan(x): Inverse tangent (arctan) of x, result in radians.

import mathangle_in_radians = math.radians(90)  # Convert 90 degrees to radiansprint(math.sin(angle_in_radians))  # Output: 1.0print(math.cos(angle_in_radians))  # Output: 6.123233995736766e-17 (approximately 0)print(math.tan(angle_in_radians))  # Output: 1.633123935319537e+16 (very large)

3. Logarithmic Functions

  • math.log(x, base): Returns the logarithm of x to the given base. If no base is provided, it calculates the natural logarithm (base e).

  • math.log10(x): Returns the base-10 logarithm of x.

  • math.log2(x): Returns the base-2 logarithm of x.

import mathprint(math.log(100))      # Natural logarithm of 100 (base e), Output: 4.605170186print(math.log(100, 10))  # Logarithm base 10, Output: 2.0print(math.log2(8))      # Logarithm base 2, Output: 3.0

4. Power and Exponentiation

  • math.pow(x, y): Returns x raised to the power of y (equivalent to x ** y).

  • math.sqrt(x): Returns the square root of x.

  • math.exp(x): Returns e raised to the power of x (e^x).

import mathprint(math.pow(2, 3))    # Output: 8.0print(math.sqrt(16))    # Output: 4.0print(math.exp(2))      # Output: 7.3890560989 (approximately e^2)

5. Factorials and Combinations

  • math.factorial(x): Returns the factorial of x (x!).

  • math.comb(n, k): Returns the number of ways to choose k items from n items without repetition (combinations).

  • math.perm(n, k): Returns the number of ways to arrange k items from n items (permutations).

import mathprint(math.factorial(5))    # Output: 120 (5! = 5 * 4 * 3 * 2 * 1)print(math.comb(5, 2))      # Output: 10 (5 choose 2)print(math.perm(5, 2))      # Output: 20 (5 permute 2)

6. Rounding and Floating Point

  • math.ceil(x): Returns the smallest integer greater than or equal to x (round up).

  • math.floor(x): Returns the largest integer less than or equal to x (round down).

  • math.trunc(x): Truncates x to the nearest integer, removing the fractional part.

import mathprint(math.ceil(4.3))   # Output: 5print(math.floor(4.7))  # Output: 4print(math.trunc(4.7))  # Output: 4

7. Hyperbolic Functions

The math module also provides functions for hyperbolic operations.

  • math.sinh(x): Hyperbolic sine of x.

  • math.cosh(x): Hyperbolic cosine of x.

  • math.tanh(x): Hyperbolic tangent of x.

import mathx = 1.0print(math.sinh(x))  # Output: 1.1752011936438014print(math.cosh(x))  # Output: 1.5430806348152437print(math.tanh(x))  # Output: 0.7615941559557649

8. Angle Conversion

  • math.degrees(x): Converts angle x from radians to degrees.

  • math.radians(x): Converts angle x from degrees to radians.

import mathangle_in_degrees = 90angle_in_radians = math.radians(angle_in_degrees)  # Converts degrees to radiansprint(angle_in_radians)  # Output: 1.5707963267948966 (?/2)angle_in_degrees_back = math.degrees(angle_in_radians)  # Converts radians back to degreesprint(angle_in_degrees_back)  # Output: 90.0

9. Other Useful Functions

  • math.isqrt(x): Returns the integer square root of x.

  • math.gcd(a, b): Returns the greatest common divisor of a and b.

import mathprint(math.isqrt(16))  # Output: 4print(math.gcd(56, 98))  # Output: 14

Conclusion

The math module in Python is a powerful library that provides essential mathematical operations for performing calculations related to trigonometry, logarithms, exponentiation, combinatorics, and more. Whether you're working with simple arithmetic or more complex scientific computations, this module provides a wide range of functions to handle mathematical tasks efficiently.

Let me know if you'd like more examples or need help with a specific mathematical operation!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql