In this tutorial we explain Python Tutorial – Mathematical Functions. In the above tutorial we shared video tutorial. Python includes the following functions that perform mathematical calculations. Python has a built-in library called “math” that provides a variety of mathematical functions. Here are some examples of commonly used mathematical functions in Python:
math.sqrt(x)
: This function returns the square root of a number.math.exp(x)
: This function returns the value of e (the base of natural logarithms) raised to the power of x.math.log(x)
: This function returns the natural logarithm of x.math.log10(x)
: This function returns the base-10 logarithm of x.math.sin(x)
,math.cos(x)
,math.tan(x)
: These functions return the sine, cosine, and tangent of x, respectively. The input x is in radians.math.asin(x)
,math.acos(x)
,math.atan(x)
: These functions return the arcsine, arccosine, and arctangent of x, respectively. The output will be in radians.math.degrees(x)
: This function converts an angle from radians to degrees.math.radians(x)
: This function converts an angle from degrees to radians.math.floor(x)
: This function rounds a number down to the nearest integer.math.ceil(x)
: This function rounds a number up to the nearest integer.
To use these functions, you will need to import the math library at the beginning of your Python script by using import math
. Then, you can call the function by using the syntax math.function_name(x)
.
It’s worth to notice that Python also has a module called cmath
for complex numbers, and decimal
for floating-point arithmetic with high precision and correct rounding.
Number abs() Method
The abs() method returns the absolute value og x i.e., the positive distance between x and zero.
Syntax:
abs(x)
x – This is a numeric expression
Number ceil() Method
The ceil() method returns the ceiling value of x i.e., the smallest integer not less than x.
Syntax:
import math
math.ceil(x)
x – This is a numeric expression
Number floor() Method
The floor() method returns the floor of x i.e., the largest integer not greater than x.
Syntax:
import math
math.floor(x)
Number exp() Method
The exp() method returns exponential of x: e pow(x)
Syntax:
import math
math.exp(x)
Number max() Method
The max() method returns the largest of its arguments. i.e., the value closest to positive
infinity.
Syntax:
max(x,y,z….)
Number min() Method
Returns the smallest of its arguments ie, the value closest to negative infinity.
Syntax:
min(x,y,z,…….)
Number pow() Method
Returns the value of x pow y
Syntax:
import math
math.pow(x,y)
Number round() Method
random() is a built-in function in Python.
It returns x rounded to n digits from decimal point.
Syntax:
round(x,[,n])
Number sqrt() Method
The sqrt method return the square root of x for x>0.
Syntax:
import math
math.sqrt(x)
Let us see a simple program in Python using mathematical functions
Save it in desktop and run the code.
Random Number Functions
Random numbers are used for games, simulations testing and privacy applications.
Python includes the following functions that are commonly used.
Number choice() Method
Returns a random item from a list, tuple or string.
Number randrange() Method
Returns a randomly selected element from range(start,stop, step)
Number random() Method
Returns a random floating point number in the range[0.0,1.0]
Number seed() Mehod
initializes the basic random number generator.
Number shuffle() Method
Randomizes the items of a list in place.
Number uniform Method
Returns a random float r, such that x is less than or eqaul to r and r is less than y.
Let us see a program in Python using random() functions.
save it in desktop and run the code.
Trignometric Functions
Python includes the following functions
acos(), asin(), atan(), atan2(), cos(), hypot(), sin(), tan(), sin(), degrees() and radians().
Let us see a program in Python using Trignometric functions.
Save it in desktop and run the code.
This is about Number functions in Python.
Leave a Reply