You are currently viewing Working with Math Module of Python
Math Module in Python

Working with Math Module of Python

🖐🖐Hey!!, are you ready to enhance your python skills?? If Yes!!, you’re most welcome in this domain (www.bittutech.com)👍. I hope you all are very well and have enjoyed my previous article–

Today’s Blog post is dedicated to how to complete tasks by using math module of Python Programming Language.

So, let’s begin to read a new and interesting python concept–:

Python makes available many more functions through modules in its standard library.

Python’s standard library is a collection of many modules for different functionalities.

e.g., module time offers time related functions, module string offers functions for string manipulation and so on.

Python’s standard library provides a module namely math for math related functions that work with all number types except for complex numbers.

In order to work with functions of math module, you’ll need to first import it into your program by giving statement as given below Python script:

import math   (This is technique to use math library in your python program)

Then you can use math library’s functions as math.<function-name> .

Let’s see some useful math functions that you can use in your program.

1. Ceil–:

math.ceil ( )

The ceil ( ) function returns the smallest integer from float data type but not less than the float number that you are given.

For example–:

import math

num= float(input("Enter any float number"))

print("Function returns smallest integer but not less than number that you are given", math.ceil(num))

Output–:

2. Sqrt–:

math.sqrt ( )

The sqrt( ) function returns the square root of the given number.

For example–:

import math

Number= 9

print("Squre root of", Number, "is", math.sqrt(Number))

Output–:

Square root of 9 is 3.0

3. Exp–:

math.exp( )

The exp( ) function returns the natural logarithm ‘e’ raised to the given number power.

For example–:

import math

Num= float(input("Enter the number that you want to get the exponentiation--:"))

print("The number return the natural logarithm e raised to the power that number--:",math.exp(Num))

Output–:

4. Fabs–

math.fabs( )

The fabs( ) function returns the absolute value of the given number.

For example–;

import math

num= 2.13

print(math.fabs(num))

Output–:

2.13

5. Floor–:

math.floor( )

The floor( ) function returns the largest integer but not greater than the number that you are given.

For example–:

import math 
print(math.floor(12.05))

Output–:

12

6. Log–:

math.log(num, [base])

The log( ) function returns the log value and assign base value. The function returns the natural logarithm for num. A domain error occurs if num is negative and a range error occurs if the argument num is zero.

For example–:

import math 
print(math.log(16,2))

Output–:

4.0

7. Log10( )

math.log10(num)

The log10( ) function returns the base 10 logarithm for num. A domain error occurs if num is negative and a range error occurs is the argument is zero.

For example–:

import math
print (math.log10(10))

Output–:

1.0

8. Pow–:

math.pow(base,exp)

The pow( ) function returns the base raised to exp power i.e., base exp. A domain error occurs if base=0 and exp<=0; also if base<0 and exp is not integer.

For example–:

import math
print(math.pow(3,0))

Output–:

1.0

9. Sin–:

math.sin(arg)

The sin( ) function returns in the sine of argument. The value of arg must be in radians.

For example–:

import math
print(math.sin(1.571))

Output–:

0.9999999792586128

Before passing argument make sure that the argument must be in radians. Here, in above example you have to change 90° in radians by multiplying 90° to π/180° (90° × π/180°) and the answer is 1.571. Now put the radian value of 90° in sin( ) function and the final answer is 0.9999999792586128 and this value is too much closer to 1. And we already know that the the value of sin90° is 1.

10. Cos–:

math.cos(arg)

The cos( ) function returns the cosine of arg. The value of arg must be in radians.

For example–:

import math
print(math.cos(1.571))

Output–:

0

The output is 0 because we already know that the value of cos90° is equal to 0.


Last words -:

You should only do those things who you want to do.

Prajjwal Singh
Share post

Prajjwal Singh

Tech Blogger || Web developer || Computer Networking Enthusiast || Microsoft SQL Database Management Expert || Software Debugger || Learned DOS OS Structure

Leave a Reply