Lab 9
Lab 9
LAB # 9
MATHEMATICAL FUNCTIONS
OBJECTIVE
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 (*).
Math Methods
Method Description
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:
Output:
2
6
-5
23
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:
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:
Example
Find the sine of different degrees:
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:
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:
Output:
6
24
120
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:
Output:
3
6
12
Lab Task