Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Python Math Functions

Last Updated on October 31, 2023 by Ankit Kochar

The Python programming language offers a variety of built-in functions designed for mathematical operations. These functions are invaluable for tackling intricate mathematical challenges across scientific and engineering domains. Python boasts an uncomplicated and user-friendly syntax that simplifies mathematical computations. In this article, we will explore Python’s mathematical functions, elucidate their usage, and furnish examples of frequently employed mathematical functions.

Basic Math Operations in Python

Before delving into more advanced Python math functions, it’s crucial to grasp the fundamental mathematical operations available in Python, including 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 serves as a valuable resource, offering an extensive array of mathematical functions that extend beyond the fundamental arithmetic operations. This module proves indispensable for conducting intricate mathematical calculations, including trigonometric, logarithmic, and exponential functions, among others. By importing and utilizing the math module, Python programmers gain access to a comprehensive assortment of functions and constants to enhance the mathematical capabilities of their 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 functions and the math module provide a robust set of tools for performing complex mathematical operations within Python programs. These functions go beyond basic arithmetic, encompassing trigonometric, logarithmic, exponential, and other advanced mathematical operations. Understanding and utilizing these functions is essential for scientific, engineering, and mathematical applications in Python.

Frequently Asked Questions (FAQs) related to Python Math Functions

FAQs related to Python Math Functions are as follows:

1. How do I use the math module in Python?
You can use the math module in Python by importing it using the import math statement. After importing, you can access its functions and constants using the math.function_name() syntax.

2. What are some common mathematical functions provided by the math module?
Common functions include trigonometric functions (e.g., sin, cos, tan), logarithmic functions (e.g., log, log10), exponential functions (e.g., exp, pow), and constants (e.g., pi, e).

3. Can I perform square roots and exponentiation using the math module?
Yes, you can perform square roots using math.sqrt() and exponentiation using math.pow() or the ** operator.

4. How do I round numbers using the math module?
The math.ceil() function rounds a number up to the nearest integer, while math.floor() rounds it down. The round() function provides standard rounding to the nearest integer.

5. Are there trigonometric functions in the math module for radians and degrees?
Yes, the math module provides trigonometric functions for both radians (e.g., math.sin, math.cos) and degrees (e.g., math.degrees, math.radians).

6. What is the mathematical constant "pi" in Python’s math module?
The mathematical constant "pi" in Python is represented as math.pi and is approximately equal to 3.141592653589793.

7. Can the math module handle complex numbers?
No, the math module is designed for real numbers and cannot handle complex numbers. For complex number operations, you should use the cmath module.

Leave a Reply

Your email address will not be published. Required fields are marked *