# Python Math – Tutorial with Examples

In Python, the “math” module provides mathematical operations and functions. This module provides functions for basic mathematical operations such as addition, subtraction, multiplication, and division, as well as more advanced mathematical functions such as trigonometric functions, logarithmic functions, and exponential functions.

## Using the Math Module

To use the “math” module in your Python code, you need to import it using the “import” statement, as shown below:

```import math
```

## Basic Mathematical Operations

The “math” module provides functions for performing basic mathematical operations, such as addition, subtraction, multiplication, and division. For example:

```import math

print(math.subtract(4, 3)) # Output: 1
print(math.multiply(3, 4)) # Output: 12
print(math.divide(4, 3)) # Output: 1.3333333333333333
```

Note that these functions are not available in the “math” module. Instead, you can perform these operations using the standard “+”, “-“, “*”, and “/” operators, respectively.

## Trigonometric Functions

The “math” module provides functions for performing trigonometric operations, such as sine, cosine, and tangent. For example:

```import math

print(math.sin(angle)) # Output: 0.49999999999999994
print(math.cos(angle)) # Output: 0.8660254037844386
print(math.tan(angle)) # Output: 0.5773502691896257
```

In this example, the “math.radians” function is used to convert the angle from degrees to radians. The “math.sin”, “math.cos”, and “math.tan” functions are then used to calculate the sine, cosine, and tangent of the angle, respectively.

## Logarithmic Functions

The “math” module provides functions for performing logarithmic operations, such as natural logarithm and base-10 logarithm. For example:

```import math

print(math.log(100)) # Output: 4.605170185988092
print(math.log10(100)) # Output: 2.0
```

In this example, the “math.log” function calculates the natural logarithm of 100, and the “math.log10” function calculates the base-10 logarithm of 100.

## Exponential Functions

The “math” module provides functions for performing exponential operations, such as raising a number to a power. For example:

```import math

print(math.pow(2, 3)) # Output: 8.0
print(math.exp(3)) # Output: 20.08
```

## Constants

The “math” module also provides some mathematical constants, such as “pi” and “e”. For example:

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

In this example, the “math.pi” constant represents the value of pi (approximately 3.14), and the “math.e” constant represents the value of Euler’s number (approximately 2.72).

## Rounding Functions

The “math” module also provides functions for rounding numbers. For example:

```import math
print(math.ceil(2.2)) # Output: 3
print(math.floor(2.8)) # Output: 2
print(round(2.5)) # Output: 3
```

In this example, the “math.ceil” function rounds the number up to the nearest integer, the “math.floor” function rounds the number down to the nearest integer, and the “round” function rounds the number to the nearest integer. Note that the “round” function is a built-in function in Python, not a function in the “math” module.

## Conclusion

In this article, we’ve covered the basics of the “math” module in Python, including its functions for basic mathematical operations, trigonometric functions, logarithmic functions, exponential functions, constants, and rounding functions. With the help of the “math” module, you can perform various mathematical operations and calculations in your Python code easily and efficiently.