How to Convert Decimal to Other Data Types in Python


In this example we will show how to convert decimal to other data types (Binary / Octal / Hexadecimal) in Python.

Source Code


#! /usr/bin/env python3
# -*- coding: utf-8 -*-

# Get user input decimal number
num = int(input("please input a num:"))

print("Decimal number is:", num)
print("Convert to binary:", bin(num))
print("Convert to octal:", oct(num))
print("Convert to hexadecimal:", hex(num))

Output:

please input a num:24
Decimal number is: 24
Convert to binary: 0b11000
Convert to octal: 0o30
Convert to hexadecimal: 0x18
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments