Python Math
Python features a number of built-in math
functions, including a large math module, which enables mathematical tasks to be carried out on numbers.
Built-in Math Functions
Example 1 :- The functions min()
and max()
are used to find in an iterable the lowest or highest value :
x = min(5, 10, 25)
y = max(5, 10, 25)
print(x)
print(y)
Output :-
25
Example 2 :- The function abs()
returns the absolute value (positive) of the provided number. It removes signs (positive (+) sign or negative (-) sign) from the given number.
x = abs(-7.25)
print(x)
Output :-
The function pow(x,y)
returns the value x to the power y (xy) :
Example 3 :- Return the value of 4 to the power of 3 (same as 4 * 4 * 4):
x = pow(4, 3)
print(x)
Output :-
Related Links
The Math Module
Python additionally has an integrated math
module which extends the mathematical list functions.
The math
module has to be imported to use it :
import math
You can start with the methods and constants of the module when you have imported the math
module.
Example 1 :- For example math.sqrt()
returns a number's square root :
import math
x = math.sqrt(64)
print(x)
Output :-
Example 2 :- The function math.ceil()
rounds a number up to its nearest integer, and the method math.floor()
rounds down to its closest integer and returns the result :
#Import math library
import math
#Round a number upward to its nearest integer
x = math.ceil(1.4)
#Round a number downward to its nearest integer
y = math.floor(1.4)
print(x)
print(y)
Output :-
1
Example 3 :- The math.pi
constant, PI value yields (3.14...) :
import math
x = math.pi
print(x)
Output :-
Related Links