10 minutes read

Python Program to convert Decimal to Binary Octal and Hexadecimal

Summary:Conversion of decimal to binary, octal and hexadecimal is one of the most popular questions asked during interviews. This tutorial explains the maths behind it and coding in detail.

Writing a program for number conversion in other languages might require manual calculation and mathematics. But, Python is awesome. Here is a simple Python program to convert decimal number to binary, octal and hexadecimal easily using inbuilt functions.

Decimal numbers are the numbers with base 10 which we use in our daily life. Binary numbers have base 2 and octal and hexadecimal have base 8 and 16 respectively.

The binary number has a prefix 0b, octal has 0o and hexadecimal have 0x. These prefixes separate them from decimals.

Here are references for further reading on conversion.

Python Program: Decimal to Binary Number

Copy

num = int(input("Enter the decimal number: "))

#Decimal to binary
binary = bin(num)
print("Binary Equivalent of {0} is {1}".format(num, binary))

#Binary to decimal
decimal = int(str(binary), 2)
print("Decmal Equivalent of {0} is {1}".format(binary, decimal))

Output

Enter the decimal number: 15 Binary Equivalent of 15 is 0b1111 Decmal Equivalent of 0b1111 is 15

The bin() function is used to convert decimal number to binary number in Python.

We take the input and convert it to the integer type and then pass it to the bin() function which returns the binary equivalent of the decimal.

To convert the binary number back to integer we use the int() function. The int() function takes two arguments first is a string of the number and second argument is base of that number type.

Python Program: Decimal to Octal Number

Copy

num = int(input("Enter the decimal number: "))

#Decimal to octal
octal = oct(num)
print("Octal Equivalent of {0} is {1}".format(num, octal))

#Octal to decimal
decimal = int(str(octal), 8)
print("Decmal Equivalent of {0} is {1}".format(octal, decimal))

Output

Enter the decimal number: 15 Octal Equivalent of 15 is 0o17 Decmal Equivalent of 0o17 is 15

The program works the same as above but we use oct() function which converts decimal number to octal number in Python.

To convert octal back to decimal we pass the octal number of type string and base 8 to int() function.

Python Program: Decimal to Hexadecimal Number

Copy

num = int(input("Enter the decimal number: "))

#Decimal to Hexadecimal
hexadecimal = hex(num)
print("Hexadecimal Equivalent of {0} is {1}".format(num, hexadecimal))

#Hexadecimal to decimal
decimal = int(str(hexadecimal), 16)
print("Decmal Equivalent of {0} is {1}".format(hexadecimal, decimal))

Output

Enter the decimal number: 15 Hexadecimal Equivalent of 15 is 0xf Decmal Equivalent of 0xf is 5

The hex() function converts a decimal number to hexadecimal in Python.

To convert the hexadecimal number back to decimal we use int() function and pass two arguments. The first argument is hexadecimal of string type and the second argument is its base (16).

Related Python Examples:

\

Join Our Youtube Channel

Subscribe