TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Math Module

Python Math Module with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, basics, data types, operators, etc.

<< Back to PYTHON

Python Math Module

Python math module is defined as the most famous mathematical functions, which includes trigonometric functions, representation functions, logarithmic functions, etc. Furthermore, it also defines two mathematical constants, i.e., Pie and Euler number, etc.

Pie (n): It is a well-known mathematical constant and defined as the ratio of circumstance to the diameter of a circle. Its value is 3.141592653589793.

Euler's number(e): It is defined as the base of the natural logarithmic, and its value is 2.718281828459045.

There are different math modules which are given below:

math.log()

This method returns the natural logarithm of a given number. It is calculated to the base e.

Example

import math  
number = 2e-7  # small value of of x  
print('log(fabs(x), base) is :', math.log(math.fabs(number), 10))  

Output:

log(fabs(x), base) is : -6.698970004336019
<

math.log10()

This method returns base 10 logarithm of the given number and called the standard logarithm.

Example

import math  
x=13  # small value of of x  
print('log10(x) is :', math.log10(x))  

Output:

log10(x) is : 1.1139433523068367 

math.exp()

This method returns a floating-point number after raising e to the given number.

Example

import math  
number = 5e-2  # small value of of x  
print('The given number (x) is :', number)  
print('e^x (using exp() function) is :', math.exp(number)-1)  

Output:

The given number (x) is : 0.05
e^x (using exp() function) is : 0.05127109637602412

math.pow(x,y)

This method returns the power of the x corresponding to the value of y. If value of x is negative or y is not integer value than it raises a ValueError.

Example

import math
number = math.pow(10,2)
print("The power of number:",number)

Output:

The power of number: 100.0

math.floor(x)

This method returns the floor value of the x. It returns the less than or equal value to x.

Example:

import math
number = math.floor(10.25201)
print("The floor value is:",number)

Output:

The floor value is: 10

math.ceil(x)

This method returns the ceil value of the x. It returns the greater than or equal value to x.

import math
number = math.ceil(10.25201)
print("The floor value is:",number)

Output:

The floor value is: 11

math.fabs(x)

This method returns the absolute value of x.

import math
number = math.fabs(10.001)
print("The floor absolute is:",number)

Output:

The absolute value is: 10.001

math.factorial()

This method returns the factorial of the given number x. If x is not integral, it raises a ValueError.

Example

import math
number = math.factorial(7)
print("The factorial of number:",number)

Output:

The factorial of number: 5040

math.modf(x)

This method returns the fractional and integer parts of x. It carries the sign of x is float.

Example

import math
number = math.modf(44.5)
print("The modf of number:",number)

Output:

The modf of number: (0.5, 44.0)

Python provides the several math modules which can perform the complex task in single-line of code. In this tutorial, we have discussed a few important math modules.


Next TopicPython Tutorial




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf