Python Program to Convert Decimal to Binary, Octal, and Hexadecimal – 2024

Hey guys, in this blog we will see a Python Program to Convert Decimal to Binary, Octal, and Hexadecimal.

Code

# Python Program to Convert Decimal to Binary, Octal, and Hexadecimal
dec = int(input('Enter a positive number -> '))

print(f"{dec} in binary is {bin(dec)}")
print(f"{dec} in octal is {oct(dec)}")
print(f"{dec} in hexadecimal is {hex(dec)}")

Output

Enter a positive number -> 2
2 in binary is 0b10
2 in octal is 0o2
2 in hexadecimal is 0x2
Enter a positive number -> 8
8 in binary is 0b1000
8 in octal is 0o10
8 in hexadecimal is 0x8
Enter a positive number -> 756
756 in binary is 0b1011110100
756 in octal is 0o1364
756 in hexadecimal is 0x2f4

We have simply used:

  • bin() to convert decimal to binary.
  • oct() to convert octal to binary.
  • hex() to convert hexadecimal to binary.

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 520

Subscribe to our Newsletter

Subscribe to our newsletter and receive all latest projects...

Leave a Reply

Your email address will not be published. Required fields are marked *