Class 12: Python Math Inbuilt Functions 3

gcd() 

This function is used to compute the greatest common divisor of 2 numbers mentioned in its arguments. This function works in python 3.5 and above.

Example:

import math
print("gcd function")
a=15
b=5
print(math.gcd(a,b))
a=100
b=15
print(math.gcd(a,b))
a=4
b=3
print(math.gcd(a,b))
a=4
b=2
print(math.gcd(a,b))

Output:

gcd function
5
5
1
2
>>>

abs()

The abs() is one of the most popular Python built-in functions, which returns the absolute value of a number. A negative value’s absolute is that value is positive.

>>> abs(-7)
7

>>> abs(7)
7

>>> abs(0)

bin()

bin() converts an integer to a binary string.

>>> bin(10)
‘0b1010’
>>> print(bin(10))
0b1010
>>> bin(7)
‘0b111’
>>> bin(2)
‘0b10’
>>> bin(5)
‘0b101’
>>>

Note:binary form of float will give an error.

oct()

oct() function given the octal form of a number.

>>> oct(10)
‘0o12’
>>> print(oct(10))
0o12
>>> print(oct(4))
0o4
>>> print(oct(15))
0o17
>>>

hex()

Hex() Python built-in functions, converts an integer to hexadecimal.

>>> hex(16)
‘0x10’

>>> hex(False)
‘0x0’