Converting a number from octal binary and hexadecimal system to decimal number system.
n1 = 0o17 # representation of octal numbers
n2 = 0b1110010 # representation of binary numbers
n3 = 0x1c2 # representation of hexadecimal number
We can do this with the help of int() method like int(n1), int(n2), int(n3).
# representation of octal numbers
n1 = 0o17
# representation of binary numbers
n2 = 0b1110010
# representation of hexadecimal number
n3 = 0x1c2
print("Octal to Decimal : ", int(n1))
print("Binary to Decimal : ", int(n2))
print("Hex to Decimal : ", int(n3))
O/P :
Octal to Decimal : 15
Binary to Decimal : 114
Hex to Decimal : 450
Converting a decimal integer to octal, binary and hexadecimal system.
bin() : For decimal to binary conversion.
oct() : For decimal to octal conversion.
hex() : For decimal to hexadecimal conversion
num = 10
print("10 in octal form : ", oct(num))
print("10 in hexadecimal form : ", hex(num))
print("10 in binary form : ", bin(num))
O/P :
10 in octal form : 0o12
10 in hexadecimal form : 0xa
10 in binary form : 0b1010
You can remove the first two character (0o, 0x, 0b) for each form to get the formatted result. Let us modify the above code to achieve this.
Note : We just did some casting here, we’ll learn this in upcoming lessons.
num = 10
print("10 in octal form : ", str(oct(num)).replace('0o', ''))
print("10 in hexadecimal form : ", str(hex(num)).replace('0x', ''))
print("10 in binary form : ", str(bin(num)).replace('0b', ''))
O/P :
10 in octal form : 12
10 in hexadecimal form : a
10 in binary form : 1010
Cool Mathematical methods
ceil(x) : It raises x value to the next higher integer value. For example, ceil(4.5) gives 5.
floor(x) : It decreases x value to previous integer value. For example, floor(4.5) gives 4.
degrees(x) : It converts angle value x from radians to degrees.
radians(x) : It converts x value from degree to radians.
sin(x) : It gives a sine value of x.
cos(x) : It gives a cosine value of x.
tan(x) : It gives a tan value of x.
exp(x) : It gives exponential of x.
fabs(x) : It gives absolute value of x. Like fabs(-4.53) gives 4.53.
factorial(x) : It gives the factorial of x.
fmod(x,y) : It gives remainder of division of x & y. Like, fmod(13.5,3) gives 1.5.
fsum(val) : It gives the accurate sum of floating point values.
log10(x) : It gives base-10 logarithm of x.
sqrt(x) : It gives the square-root of number x.
pow(x,y) : It raises x value to the power y.
pow(x,y,z) : It raises x value to the power y mod z.
gcd(x,y) : It is used to find the greatest common divisor of x & y.
trunc(x) : It returns real value of x is truncated to integer value. Like trunc(43.545) returns 43.
isnan(x) : It returns True if x is not a number.
eval(expression) : It returns evaluated arithmetic expression. Like, eval(3*7) gives 21.
# Illustration of mathematical methods :
# We need to import python in-built module 'math' to access mathematical methods
import math as m # creating 'm' as alias(nickname) for 'math'
print("ceil(4.5) : ", m.ceil(4.5))
print("floor(4.5) : ", m.floor(4.5))
print("degrees(180/3.14) : ", m.degrees(180/3.14))
print("radians(90) : ", m.radians(90))
print("sin(30) : ", m.sin(30))
print("cos(45) : ", m.cos(45))
print("tan(60) : ", m.tan(60))
print("exp(2) : ", m.exp(2))
print("fabs(-2.86) : ", m.fabs(-2.86))
print("factorial(6) : ", m.factorial(6))
print("fmod(17,2) : ", m.fmod(17,2))
print("log10(2) : ", m.log10(2))
print("sqrt(20) : ", m.sqrt(20))
print("pow(2,5) : ", m.pow(2,5))
print("gcd(50,100) : ", m.gcd(50,100))
print("trunc(7.677) : ", m.trunc(7.677))
print("isnan(2) : ", m.isnan(2))
print("eval('(2*3)+(9/3)') : ", eval("(2*3)+(9/3)"))
O/P :
ceil(4.5) : 5
floor(4.5) : 4
degrees(180/3.14) : 3284.4714370556744
radians(90) : 1.5707963267948966
sin(30) : -0.9880316240928618
cos(45) : 0.5253219888177297
tan(60) : 0.320040389379563
exp(2) : 7.38905609893065
fabs(-2.86) : 2.86
factorial(6) : 720
fmod(17,2) : 1.0
log10(2) : 0.3010299956639812
sqrt(20) : 4.47213595499958
pow(2,5) : 32.0
gcd(50,100) : 50
trunc(7.677) : 7
isnan(2) : False
eval(‘(2*3)+(9/3)’) : 9.0
That’s all, see you in the next lecture.