Easy Tutorial
❮ Python Sum Dictionary Ref Math Erfc ❯

Python Decimal to Binary, Octal, Hexadecimal Conversion

Python3 Examples

The following code is used to convert decimal to binary, octal, and hexadecimal:

Example (Python 3.0+)

# -*- coding: UTF-8 -*-

# Filename : test.py
# author by : www.tutorialpro.org

# Get user input for a decimal number
dec = int(input("Enter a number: "))

print("The decimal number is:", dec)
print("Converted to binary is:", bin(dec))
print("Converted to octal is:", oct(dec))
print("Converted to hexadecimal is:", hex(dec))

Executing the above code will produce the following results:

python3 test.py 
Enter a number: 5
The decimal number is: 5
Converted to binary is: 0b101
Converted to octal is: 0o5
Converted to hexadecimal is: 0x5
python3 test.py 
Enter a number: 12
The decimal number is: 12
Converted to binary is: 0b1100
Converted to octal is: 0o14
Converted to hexadecimal is: 0xc

Python3 Examples

❮ Python Sum Dictionary Ref Math Erfc ❯