English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Online Tools

Python Basic Tutorial

Python Functions

Python Data Types

Python Flow Control

O

Python Objects and Classes

Python Date and Time

Advanced Knowledge of Python

Python Reference Manual

Python Examples

Python program to convert decimal to binary, octal, and hexadecimal

In this program, you will learn to convert decimal to binary, octal, and hexadecimal and display it.To understand this example, you should understand the followingPython Programming

Python Programming Built-in Functions

Decimal is the most widely used number system. However, computers can only understand binary. The binary, octal, and hexadecimal number systems are closely related, and we may need to convert decimal to these systems.10Base (ten symbols 0-9Used to represent numbers), similarly, binary is based on2Base, octal is8Base, hexadecimal is16.

Numbers with prefixes 0b are considered binary, 0o octal, and 0x hexadecimal. For example:

60 = 0b11100 = 0o74 = 0x3c

Source Code

# Python program to convert decimal to other number systems
dec = 344
print("Decimal value", dec, "Can be converted to:")
print(bin(dec), "Binary.")
print(oct(dec), "Octal.")
print(hex(dec), "Hexadecimal.")

Output Result

Decimal value 344 Can be converted to:
0b101011000 Binary.
0o530 Octal.
0x158 Hexadecimal.

Note:To test other decimal numbers in the program, please change the value of dec in the program.

In this program, we use the built-in functions bin(), oct(), and hex() to convert the given decimal number to the corresponding number system.

These functions use integers (decimal) and return strings.

Python Examples