Python programming language provides a range of built-in functions to perform mathematical operations. These functions are used to solve complex mathematical problems in various scientific and engineering applications. Python provides a simple and easy-to-use syntax for performing mathematical calculations. This article will discuss Python math functions, how to use them, and provide examples of commonly used math functions.

## Basic Math Operations in Python

Before diving into more complex Python math functions, it is essential to understand the basic math operations in Python such as addition, subtraction, multiplication, and division.

The plus sign (+) is used for addition, the minus sign (-) for subtraction, the asterisk (*) for multiplication, and the forward-slash (/) for division. Additionally, the double asterisk (**) is used for exponentiation, and the percent sign (%) is used for modulo operations.

Here are some examples of basic math operations in Python:

a = 5 b = 3 # Addition c = a + b print("The addition of a and b is ", c) # Subtraction c = a - b print("The Subtraction of a and b is ", c) # Multiplication c = a * b print("The multiplication of a and b is ", c) # Division c = a / b print("The Division of a and b is ", c) # Exponentiation c = a ** b print("The Exponentiation of a and b is ", c) # Modulo c = a % b print("The Modulo of a and b is ", c)

**Output:**

```
The addition of a and b is 8
The Subtraction of a and b is 2
The multiplication of a and b is 15
The Division of a and b is 1.6666666666666667
The Exponentiation of a and b is 125
The Modulo of a and b is 2
```

**Explanation:**

In the above code first, we declare and assign the values of the variables. Then one by one we have performed all the Basic Math Operations such as addition, subtraction, multiplication, division, exponentiation, and modulo.

## Python Math Functions

Let us now learn about the Python Math Module which contains all the maths Functions in detail.

## What is the Math Module?

Python’s built-in math module provides a wide range of mathematical functions that are not included in basic math operations. The math module is used to perform complex mathematical operations such as trigonometric, logarithmic, and exponential functions. The math module provides a collection of functions and constants that can be imported and used in Python programs.

### How to Use the Math Module

To use the math module in a Python program, we need to import it using the import statement. Here is an example:

**Code:**

```
import math
print(math.pi)
```

**Output:**

`3.141592653589793`

**Explanation:**

This code imports the math module and prints the value of the constant pi.

## Common Math Functions in Python

The math module provides a wide range of functions for performing various mathematical operations. Here are some commonly used math functions in Python:

### 1. Absolute Value

The abs() method in Python’s Math Module returns a number’s absolute value. A number’s absolute value is defined as its distance from zero on the number line.

**Code:**

```
import math
a = -5
print("The absolute value is ", abs(a))
```

**Output:**

`The absolute value is 5`

### 2. Exponential Functions

The math module provides several functions to perform exponential operations, including exp(), pow(), and sqrt().

**exp() Function:** The exp() function returns the exponential value of a number, which is e raised to the power of the number.

**Code:**

```
import math
a = 2
print(math.exp(a))
```

**Output:**

`7.38905609893065`

**pow() Function:** The pow() function takes two arguments and returns the value of the first argument raised to the power of the second argument.

**Code:**

```
import math
a_ = 2
b_ = 3
print("The value of a_^b_ is ", math.pow(a_, b_))
```

**Output:**

`The value of a_^b_ is 8.0`

**sqrt() Function:** The sqrt() function in Python returns the square root of a number.

**Code:**

```
import math
my_var = 25
print("The square root of ", my_var, "is ", math.sqrt(my_var))
```

**Output:**

`The square root of 25 is 5.0`

### 3. Logarithmic Functions

The math module provides several functions to perform logarithmic operations, including log(), log10(), and log2().

**log() Function:** The log() function in Python returns the natural logarithm of a number.

**Code:**

```
import math
a = 10
print(math.log(a))
```

**Output:**

`2.302585092994046`

**log10() Function:** The log10() in Python function returns the base-10 logarithm of a number.

**Code:**

```
import math
a = 100
print("The value of log10(100) is ", math.log10(a))
```

**Output:**

`The value of log10(100) is 2.0`

**log2() Function:** The log2() function in Python returns the base-2 logarithm of a number.

**Code:**

```
import math
my_var = 8
print("The value of log2(my_var) is ", math.log2(my_var))
```

**Output:**

`The value of log2(my_var) is 3.0`

### 4. Trigonometric Functions

The math module provides several functions to perform trigonometric operations, including sin(), cos(), tan(), asin(), acos(), and atan().

**sin() Function:** The sin() function returns the sine of an angle in radians.

**Code:**

```
import math
my_value = math.pi / 4
print("The sine of angle my_value is ", math.sin(my_value))
```

**Output:**

`The sine of angle value is 0.7071067811865476`

**cos() Function:** The cos() function returns the cosine of an angle in radians.

**Code:**

```
import math
my_var = math.pi / 4
print("The cosine of angle my_var is ", math.cos(my_var))
```

**Output:**

`The cosine of angle my_var is 0.7071067811865476`

**tan() Function:** The tan() function returns the tangent of an angle in radians.

**Code:**

```
import math
a = math.pi / 4
print("The tan of angle a is", math.tan(a))
```

**Output:**

`The tan of angle a is 0.9999999999999999`

**asin() Function:** The asin() function in Python returns the inverse sine of a number in radians.

**Code:**

```
import math
my_var = 0.5
print("The inverse sine of my_var is ", math.asin(my_var))
```

**Output:**

`The inverse sine of my_var is 0.5235987755982989`

**acos() Function:** The acos() function returns the inverse cosine of a number in radians.

**Code:**

```
import math
a = 0.5
print("The inverse cosine of a is ",math.acos(a))
```

**Output:**

`The inverse cosine of a is 1.0471975511965979`

**atan() Function:** The atan() function returns the inverse tangent of a number in radians.

**Code:**

```
import math
my_var = 1
print("The inverse tan of my_var is ", math.atan(my_var))
```

**Output:**

`The inverse tan of my_var is 0.7853981633974483`

### 5. Hyperbolic Functions

The math module provides several functions to perform hyperbolic operations, including sinh(), cosh(), and tanh().

**sinh() Function:** The sinh() function in Python returns the hyperbolic sine of a number.

**Code:**

```
import math
var1 = 2
print("The value of sinh of var1 is", math.sinh(var1))
```

**Output:**

`The value of sinh of var1 is 3.6268604078470186`

**cosh() Function:** The cosh() function in Python’s “math” module returns the hyperbolic cosine of a number.

**Code:**

```
import math
var1 = 2
print("The value of cosh of var1 is ", math.cosh(var1))
```

**Output:**

`The value of cosh of var1 is 3.7621956910836314`

**tanh() Function:** The tanh() function in Python’s “math” module returns the hyperbolic tangent of a number.

**Code:**

```
import math
a = 2
print("The value of hyperbolic tan of a is ", math.tanh(a))
```

**Output:**

`The value of hyperbolic tan of a is 0.9640275800758169`

### 6. Constants

The math module also provides several useful mathematical constants, including pi, e, tau, and inf.

**pi Constant:** The pi constant represents the value of π.

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

**e Constant:** The e constant represents the value of e.

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

**tau Constant:** The tau constant represents the value of τ, which is equal to 2π.

```
import math
print(math.tau) # Output: 6.283185307179586
```

**inf Constant:** The inf constant represents infinity.

```
import math
print(math.inf) # Output: inf
```

### 7. Rounding Functions

The math module provides several functions to perform rounding operations, including ceil(), floor(), trunc(), and round().

**ceil() Function:** The ceil() function returns the smallest integer greater than or equal to a number.

**Code:**

```
import math
var1 = 2.3
print("The ceil of ", var1, "is ", math.ceil(var1))
```

**Output:**

`The ceil of 2.3 is 3`

**floor() Function:** The floor() function returns the largest integer less than or equal to a number.

**Code:**

```
import math
var1 = 2.7
print("The floor of ", var1, "is ", math.floor(var1))
```

**Output:**

`The floor of 2.7 is 2`

**trunc() Function:** The trunc() function returns the integer part of a number.

**Code:**

```
import math
a = 2.7
print(math.trunc(a))
```

**Output:**

`2`

**round() Function:** The round() function returns the rounded value of a number.

**Code:**

```
import math
a = 2.7
print("The rounded off value is ", round(a))
```

**Output:**

`The rounded off value is 3`

### 8. Conversion Functions

The math module provides several functions to convert between degrees and radians, including degrees() and radians().

**degrees() Function:** The degrees() function converts radians to degrees.

**Code:**

```
import math
a = math.pi / 4
print("the value of a in degrees is :", math.degrees(a))
```

**Output:**

`the value of a in degrees is : 45.0`

**radians() Function:** The radians() function converts degrees to radians.

**Code:**

```
import math
a = 45
print("the value in radians is ", math.radians(a))
```

**Output:**

`the value in radians is 0.7853981633974483`

**Conclusion**

Python’s math module is a powerful tool for performing mathematical operations in Python. It provides a wide range of functions for performing basic arithmetic operations, as well as more advanced mathematical operations like trigonometric functions, logarithmic functions, and hyperbolic functions. Additionally, it provides several useful mathematical constants and rounding and conversion functions. By understanding the functions provided by the math module, you can take your Python programming skills to the next level and perform complex mathematical calculations in your code.

## Frequently Asked Questions (FAQs)

**Ques 1. What is the difference between Python’s math module and NumPy?**

**Ans.** The math module is part of the Python standard library and provides basic mathematical functions for working with numbers. NumPy is a third-party library that is used for numerical computing and scientific computing.

**Ques 2. Can I perform trigonometric functions like sine, cosine, and tangent using Python’s math module?**

**Ans.** Yes, you can perform trigonometric functions like sine, cosine, and tangent using Python’s math module. The math.sin(), math.cos(), and math.tan() functions can be used for this purpose.

**Ques 3. How do I round a number to a specific number of decimal places using Python’s math module?**

**Ans.** The math module does not have a specific function for rounding a number to a specific number of decimal places. However, you can use the built-in round() function known as round() to achieve this. For example, round(3.1415926535, 2) will return 3.14.

**Ques 4. Can I use Python’s math module to calculate logarithms?**

**Ans.** Yes, you can use Python’s math module to calculate logarithms. The math.log() function can be used to calculate the natural logarithm, while the math.log10() function can be used to calculate the base 10 logarithm.

**Ques 5. How can I generate random numbers using Python’s math module?**

**Ans.** The math module does not have a specific function for generating random numbers. However, you can use the random module in Python to generate random numbers. For example, import random and random.random() will return a random float between 0 and 1.

**Ques 6. How can I convert degrees to radians and vice versa using Python’s math module?**

**Ans.** You can convert degrees to radians using the math.radians() function, and radians to degrees using the math.degrees() function. For example, math.radians(90) will return 1.5707963267948966, and math.degrees(1.5707963267948966) will return 90.0.

**Ques 7. How do I calculate the absolute value of a number in Python’s math module?**

**Ans.** You can calculate the absolute value of a number using the abs() function, which is a built-in function in Python. It is not a part of the math module. For example, abs(-5) will return 5.