Lab # 9: Mathematical Functions SSUET/QR/114
LAB # 9
MATHEMATICAL FUNCTIONS
OBJECTIVE
To get familiar with the concept of mathematical functions.
THEORY
The math module is used to access mathematical functions in the Python. All methods of
this functions are used for integer or real type objects, not for complex numbers.
Mathematical calculations are an essential part of most Python development.
Whether you’re working on a scientific project, a financial application, or any other type
of programming endeavor, you just can’t escape the need for math. For straightforward
mathematical calculations in Python, you can use the built-in mathematical operators,
such as addition (+), subtraction (-), division (/), and multiplication (*).
Python provides a module specifically designed for higher-level mathematical operations:
the math module. The Python math module offers you the ability to perform common and
useful mathematical calculations within your application. Here are a few practical uses
for the math module:
Calculating combinations and permutations using factorials
Calculating the height of a pole using trigonometric functions
Calculating radioactive decay using the exponential function
Solving quadratic equations
Simulating periodic functions, such as sound and light waves, using trigonometric
functions
Since the math module comes packaged with the Python release, you don’t have to install
it separately. Using it is just a matter of importing the module:
>>> import math
You can import the Python math module using the above command. After importing, you
can use it straightaway.
CE-101 : Computing Fundamentals 60
Lab # 9: Mathematical Functions SSUET/QR/114
Math Methods
Method Description
math.acos() Returns the arc cosine of a number
math.acosh() Returns the inverse hyperbolic cosine of a
number
math.asin() Returns the arc sine of a number
math.asinh() Returns the inverse hyperbolic sine of a number
math.atan() Returns the arc tangent of a number in radians
math.atanh() Returns the inverse hyperbolic tangent of a
number
math.ceil() Rounds a number up to the nearest integer
math.cos() Returns the cosine of a number
math.cosh() Returns the hyperbolic cosine of a number
math.degrees() Converts an angle from radians to degrees
math.dist() Returns the Euclidean distance between two
points (p and q), where p and q are the
coordinates of that point
math.exp() Returns E raised to the power of x
math.fabs() Returns the absolute value of a number
math.factorial() Returns the factorial of a number
math.floor() Rounds a number down to the nearest integer
CE-101 : Computing Fundamentals 61
Lab # 9: Mathematical Functions SSUET/QR/114
math.fmod() Returns the remainder of x/y
math.gcd() Returns the greatest common divisor of two
integers
math.isfinite() Checks whether a number is finite or not
math.isinf() Checks whether a number is infinite or not
math.isnan() Checks whether a value is NaN (not a number)
or not
math.isqrt() Rounds a square root number downwards to the
nearest integer
math.log() Returns the natural logarithm of a number, or
the logarithm of number to base
math.log10() Returns the base-10 logarithm of x
math.log2() Returns the base-2 logarithm of x
math.pow() Returns the value of x to the power of y
math.remainder() Returns the closest value that can make
numerator completely divisible by the
denominator
math.sin() Returns the sine of a number
math.sinh() Returns the hyperbolic sine of a number
math.sqrt() Returns the square root of a number
math.tan() Returns the tangent of a number
math.trunc() Returns the truncated integer parts of a number
CE-101 : Computing Fundamentals 62
Lab # 9: Mathematical Functions SSUET/QR/114
Python math.ceil() Method
Definition and Usage
The math.ceil() method rounds a number UP to the nearest integer, if necessary, and
returns the result.
Syntax
math.ceil(x)
Parameter Values
Parameter Description
x Required. Specifies the number to round up
Example
Round a number upward to its nearest integer:
# Import math library
import math
# Round a number upward to its nearest integer
print(math.ceil(1.4))
print(math.ceil(5.3))
print(math.ceil(-5.3))
print(math.ceil(22.6))
Output:
2
6
-5
23
CE-101 : Computing Fundamentals 63
Lab # 9: Mathematical Functions SSUET/QR/114
Python math.sqrt() Method
Definition and Usage
The math.sqrt() method returns the square root of a number.
Note: The number must be greater than or equal to 0.
Syntax
math.sqrt(x)
Parameter Values
Parameter Description
x Required. A number to find the square root of. If the
number is less than 0, it returns a ValueError. If the
value is not a number, it returns a TypeError
Example
Find the square root of different numbers:
# Import math Library
import math
# Return the square root of different numbers
print (math.sqrt(9))
print (math.sqrt(25))
print (math.sqrt(16))
CE-101 : Computing Fundamentals 64
Lab # 9: Mathematical Functions SSUET/QR/114
Python math.sin() Method
Definition and Usage
The math.sin() method returns the sine of a number.
Note: To find the sine of degrees, it must first be converted into radians with the
math.radians()
Syntax
math.sin(x)
Parameter Values
Parameter Description
x Required. The number to find the sine of. If the
value is not a number, it returns a TypeError
Example
Find the sine of different numbers:
# Import math Library
import math
# Return the sine of different values
print (math.sin(0.00))
print (math.sin(-1.23))
print (math.sin(10))
print (math.sin(math.pi))
print (math.sin(math.pi/2))
CE-101 : Computing Fundamentals 65
Lab # 9: Mathematical Functions SSUET/QR/114
Example
Find the sine of different degrees:
# Import math Library
import math
# Return the sine value of 30 degrees
print(math.sin(math.radians(30)))
# Return the sine value of 90 degrees
print(math.sin(math.radians(90)))
CE-101 : Computing Fundamentals 66
Lab # 9: Mathematical Functions SSUET/QR/114
Python math.pow() Method
Definition and Usage
The math.pow() method returns the value of x raised to power y.
If x is negative and y is not an integer, it returns a ValueError.
This method converts both arguments into a float.
Tip: If we use math.pow(1.0,x) or math.pow(x,0.0), it will always returns 1.0.
Syntax
math.pow(x, y)
Parameter Values
Parameter Description
x Required. A number which represents the base
y Required. A number which represents the exponent
Example
Find the value of 9 raised to the power of 3:
# Import math Library
import math
#Return the value of 9 raised to the power of 3
print(math.pow(9, 3))
CE-101 : Computing Fundamentals 67
Lab # 9: Mathematical Functions SSUET/QR/114
Python math.factorial() Method
Definition and Usage
The math.factorial() method returns the factorial of a number.
Note: This method only accepts positive integers.
The factorial of a number is the sum of the multiplication, of all the whole numbers, from
our specified number down to 1. For example, the factorial of 6 would be 6 x 5 x 4 x 3 x
2 x 1 = 720
Syntax
math.factorial(x)
Parameter Values
Parameter Description
x Required. A positive integer. If the number is
negative, or not an integer, it returns a ValueError.
If the value is not a number, it returns a TypeError
Example
Find the factorial of a number:
#Import math Library
import math
#Return factorial of a number
print(math.factorial(3))
print(math.factorial(4))
print(math.factorial(5))
Output:
6
24
120
CE-101 : Computing Fundamentals 68
Lab # 9: Mathematical Functions SSUET/QR/114
Python math.gcd() Method
Definition and Usage
The math.gcd() method returns the greatest common divisor of the two integers int1 and
int2.
GCD is the largest common divisor that divides the numbers without a remainder.
GCD is also known as the highest common factor (HCF).
Tip: gcd(0,0) returns 0.
Syntax
math.gcd(int1, int2)
Parameter Values
Parameter Description
int1 Required. The first integer to find the GCD for
int2 Required. The second integer to find the GCD for
Example
Find the greatest common divisor of the two integers:
#Import math Library
import math
#find the the greatest common divisor of the two integers
print (math.gcd(3, 6))
print (math.gcd(6, 12))
print (math.gcd(12, 36))
Output:
3
6
12
CE-101 : Computing Fundamentals 69
Lab # 9: Mathematical Functions SSUET/QR/114
Lab Task
1. Write a program, which shows the implementation of trigonometric functions.
2. Write a program to calculate the factorial of the input number.
3. Write a program to calculate the Log of input number.
4. Write a program to calculate the GCD of the input number.
CE-101 : Computing Fundamentals 70