Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
In computer programming, numerical systems play a crucial role in representing and manipulating data. The decimal system, also known as the base-10 system, is the one we commonly use in our everyday lives. However, in computer science, we often encounter other numerical systems like binary, octal, and hexadecimal.
-
Binary is a base-2 system that uses only two digits, 0 and 1, making it the foundation of all digital systems.
-
Octal, on the other hand, is a base-8 system that uses digits from 0 to 7.
-
Hexadecimal is a base-16 system that uses digits from 0 to 9 along with alphabets A to F, where A represents 10, B represents 11, and so on up to F representing 15.
Understanding how to convert decimal to binary, octal, and hexadecimal in Python is essential for various programming tasks.
Whether you're working on low-level programming, network protocols, or simply analyzing data, being able to convert between these numerical systems gives you a deeper understanding of the underlying data and helps you communicate effectively with computer systems.
In this tutorial, we will explore a Python program for decimal to binary, octal, and hexadecimal conversion. By the end of this tutorial, you will have a solid grasp of the concepts and the ability to apply these conversions in your own programming projects.
Decimal to Binary, Octal, Hexadecimal Conversion in Python
Code
# Function to convert decimal to binary
def decimal_to_binary(decimal):
binary = bin(decimal).replace("0b", "")
return binary
# Function to convert decimal to octal
def decimal_to_octal(decimal):
octal = oct(decimal).replace("0o", "")
return octal
# Function to convert decimal to hexadecimal
def decimal_to_hexadecimal(decimal):
hexadecimal = hex(decimal).replace("0x", "")
return hexadecimal
# Main program
if __name__ == "__main__":
decimal = int(input("Enter a decimal number: "))
binary = decimal_to_binary(decimal)
print("Binary: " + binary)
octal = decimal_to_octal(decimal)
print("Octal: " + octal)
hexadecimal = decimal_to_hexadecimal(decimal)
print("Hexadecimal: " + hexadecimal)
Output
Enter a decimal number: 550
Binary: 1000100110
Octal: 1046
Hexadecimal: 226
Explanation
We define three separate functions, decimal_to_binary(), decimal_to_octal(), and decimal_to_hexadecimal(), to perform the conversions. Each function takes a decimal number as input and returns the corresponding binary, octal, or hexadecimal representation.
-
The decimal_to_binary() function uses the bin() function in Python, which converts a decimal number to its binary representation. We remove the prefix "0b" from the result using the replace() method to obtain the binary string.
-
The decimal_to_octal() function uses the oct() function in Python, which converts a decimal number to its octal representation. We remove the prefix "0o" from the result using the replace() method to obtain the octal string.
-
The decimal_to_hexadecimal() function uses the hex() function in Python, which converts a decimal number to its hexadecimal representation. We remove the prefix "0x" from the result using the replace() method to obtain the hexadecimal string.
In the main program, we prompt the user to enter a decimal number using the input() function, and we convert it to an integer using int().
We then call each conversion function with the decimal number as an argument and store the results in separate variables: binary, octal, and hexadecimal.
Finally, we print the converted representations of the decimal number using the print() function, along with appropriate labels.
That's it! This program allows you to convert any decimal number into binary, octal, and hexadecimal in Python.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program