[Go to site: main page, start]

0% found this document useful (0 votes)
3 views19 pages

Unit 3 Math Module

The document provides an overview of mathematical functions available in Python through the 'math' module, including functions like ceil(), floor(), fabs(), factorial(), and fmod(), among others. It includes examples demonstrating the usage of these functions, such as calculating the ceiling and floor of a number, finding absolute values, and computing factorials. Additionally, it covers constants like pi and e, as well as trigonometric and hyperbolic functions.

Uploaded by

prembnmsrit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views19 pages

Unit 3 Math Module

The document provides an overview of mathematical functions available in Python through the 'math' module, including functions like ceil(), floor(), fabs(), factorial(), and fmod(), among others. It includes examples demonstrating the usage of these functions, such as calculating the ceiling and floor of a number, finding absolute values, and computing factorials. Additionally, it covers constants like pi and e, as well as trigonometric and hyperbolic functions.

Uploaded by

prembnmsrit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON PROGRAMMING

Mathematical Functions in Python


 In python a number of mathematical operations can be performed with
ease by importing a module named “math” which defines various functions
which makes our tasks easier.

 Important mathematical functions in python are:

 ceil() :- This function returns the smallest integral value greater than the
number. If number is already integer, same number is returned.

 floor() :- This function returns the greatest integral value smaller than the
number. If number is already integer, same number is returned.
EXAMPLE
# Python code to demonstrate the working of ceil() and floor()

# importing "math" for mathematical operations


import math

a = 2.3

# returning the ceil of 2.3


print ("The ceil of 2.3 is : ", end=“ ")
print ([Link](a))
o/p : The ceil of 2.3 is : 3
# returning the floor of 2.3
print ("The floor of 2.3 is : ", end=“ ")
print ([Link](a))
o/p : The floor of 2.3 is : 2
 fabs() :- This function returns the absolute value of the number.

 factorial() :- This function returns the factorial of the number.


Argument must be a positive integer.

 fmod() :- This function returns the remainder of the specified arguments.

Syntax: [Link]( x, y )
Parameters:
x any valid number (positive or negative).
y any valid number(positive or negative).
Returns: Returns the remainder in x/y as a floating point number.
EXAMPLE
# Python code to demonstrate the working of fabs() and factorial()

# importing "math" for mathematical operations


import math
a = -10.3
b= 5

# returning the absolute value.


print ("The absolute value of -10.3 is : ", end=“ ")
print ([Link](a))
o/p : The absolute value of -10.3 is : 10.3

# returning the factorial of 5


print ("The factorial of 5 is : ", end=“ ")
print ([Link](b))

o/p : The factorial of 5 is : 120


EXAMPLE

# Python program to demonstrate fmod() function

import math

# modulus of +ve integer number


print([Link](4, 5))
print([Link](43.50, 4.5))

# modulus of -ve integer number


print([Link](-17, 5))
print('%.2f' %[Link](-10, 4.78))

o/p : 4.0
3.0
-2.0
-0.44
fsum() :- This function find and return an accurate floating point sum
between some range or an iterable ( list ).

trunc() :- This function returns the integer part of the argument after
truncation.

exp(x) :- This function returns e**x.

log(a, b) :- This function returns the logarithmic value of a with base b.


With one argument , function returns the natural logarithm of a to base e.
With two arguments , function returns the logarithm of a to the given
base b.

 pow(a, b) :- This function is used to compute value of a raised to the


power b (a**b).

 sqrt() :- This function returns the square root of the number.


EXAMPLE
# Python code to demonstrate use of [Link]() function

# fsum() is found in math library


import math
o/p : 45.0
# range(10)
print([Link](range(10)))
11.0
7.99
# Integer list
arr = [1, 4, 6]
print([Link](arr))

# Floating point list


arr = [2.5, 2.4, 3.09]
print([Link](arr))
EXAMPLE
# trunc() for a positive number.
import math
print ([Link](3.5))
o/p : 3
# trunc() for a negative number. -4
import math
print [Link](-4.5)

# Python code to demonstrate the working of exp() and log()

import math

# returning the exp of 4


print ("The e**4 value is : ", end=“ ")
print ([Link](4)) o/p :The e**4 value is : 54.598150033144236
The value of log 2 with base 3 is : 0.6309297535714574
# returning the log of 2,3
print ("The value of log 2 with base 3 is : ", end=“ ")
print ([Link](2,3))
EXAMPLE
# Python code to demonstrate the working of pow() and sqrt()

# importing "math" for mathematical operations


import math

# returning the value of 3**2


print ("The value of 3 to the power 2 is : ", end=“ ")
print ([Link](3,2))

# returning the square root of 25


print ("The value of square root of 25 : ", end=“ ")
print ([Link](25))

o/p : The value of 3 to the power 2 is : 9.0


The value of square root of 25 : 5.0
pi :- This is an inbuilt constant that outputs the value of pi(3.141592).

e :- This is an inbuilt constant that outputs the value of e(2.718281).

sin() :- This function returns the sine of value passed as argument.


The value passed in this function should be in radians.

cos() :- This function returns the cosine of value passed as argument.


The value passed in this function should be in radians.

tan() :- This function returns the tangent of value passed as argument.


The value passed in this function should be in radians.

degrees() :- This function is used to convert argument value from


radians to degrees.

radians() :- This function is used to convert argument value from


degrees to radians.
EXAMPLE
# Python code to demonstrate the working of
# const. pi and e

# importing "math" for mathematical operations


import math

# returning the value of const. pi


print ("The value of const. pi is : ", end=“ ")
print ([Link])

# returning the value of const. e


print ("The value of const. e is : ", end=“ ")
print (math.e)
o/p : The value of const. pi is : 3.141592653589793
The value of const. e is : 2.718281828459045
EXAMPLE
# Python code to demonstrate the working of sin() , cos() and tan()
import math
a = [Link]/6

# returning the value of sine of pi/6


print ("The value of sine of pi/6 is : ", end=“ ")
print ([Link](a))

o/p : The value of sine of pi/6 is : 0.49999999999999994

# returning the value of cosine of pi/6


print ("The value of cosine of pi/6 is : ", end=“ ")
print ([Link](a))
o/p :The value of cosine of pi/6 is : 0.8660254037844387
EXAMPLE

# returning the value of tangent of pi/6


print ("The value of tangent of pi/6 is : ", end=“ ")
print ([Link](a))

o/p : The value of tangent of pi/6 is : 0.5773502691896257


EXAMPLE
# Python code to demonstrate the working of degrees() and radians()
import math

a = [Link]/6
b = 30

# returning the converted value from radians to degrees


print ("The converted value from radians to degrees is : ", end=“ ")
print ([Link](a))

# returning the converted value from degrees to radians


print ("The converted value from degrees to radians is : ", end=“ ")
print ([Link](b))

o/p : The converted value from radians to degrees is : 29.999999999999996


The converted value from degrees to radians is : 0.5235987755982988
asin() :- This function returns the arc sine of value passed as argument.
The value passed in this function should be in radians.

acos() :- This function returns the arc cosine of value passed as argument.
The value passed in this function should be in radians.

atan() :- This function returns the arc tangent of value passed as argument.
The value passed in this function should be in radians.

sinh() :- This function returns the hyperbolic sine of value passed as argument.
The value passed in this function should be in radians.

cosh() :- This function returns the hyperbolic cosine of value passed as


argument. The value passed in this function should be in radians.

tanh() :- This function returns the hyperbolic tangent of value passed as


argument. The value passed in this function should be in radians.
EXAMPLE
# Python code to implement the asin() , acos() and atan()
import math
a = [Link] / 6

# returning the value of arc sine of pi / 6


print ("The value of arc sine of pi / 6 is : ", end =“ ")
print ([Link](a))
# returning the value of arc cosine of pi / 6
print ("The value of arc cosine of pi / 6 is : ", end =“ ")
print ([Link](a))
# returning the value of arc tangent of pi / 6
print ("The value of arc tangent of pi / 6 is : ", end =“ ")
print ([Link](a))
o/p : The value of arc sine of pi / 6 is : 0.5510695830994463
The value of arc cosine of pi / 6 is : 1.0197267436954502
The value of tangent of pi / 6 is : 0.48234790710102493
EXAMPLE
# Python code to implement the sinh() , cosh() and tanh()

import math

a = [Link] / 6
o/p : 0.5478534738880397
1.1402383210764286
# Return the hyperbolic sine value
0.4804727781564516
print ([Link](a))

# Return the hyperbolic cosine value


print ([Link](a))

# Return the hyperbolic tangent value


print ([Link](a))

You might also like