Hex and Bin - Python Programming Exercise

This Python program prompts the user to enter a number and then displays its equivalent values in both hexadecimal and binary formats. The program continues to run, performing these conversions, until the user enters 0, at which point it terminates. The hexadecimal format represents numbers using a base of 16, utilizing digits 0-9 and letters A-F, while the binary format represents numbers using a base of 2, consisting of only the digits 0 and 1. This program is a great way to practice number system conversions and get familiar with Python's built-in functions for handling different bases. The program repeatedly processes user input and demonstrates how to convert decimal numbers into binary and hexadecimal formats efficiently. It's an excellent example of using loops in Python to handle repetitive tasks and conditional statements to control the program's flow. By using Python’s built-in functions like bin() and hex(), the program simplifies the conversion process and provides a practical way to work with numerical representations in computing.

 Category

Python Data Types

 Exercise

Hex And Bin

 Objective

Develop a Python program to prompt the user for a number and display it in both hexadecimal and binary. It must repeat until the user enters 0.

 Example Python Exercise

 Copy Python Code
# Repeat until the user enters 0
while True:
    # Prompt the user for a number
    number = int(input("Enter a number (0 to stop): "))
    
    # If the number is 0, exit the loop
    if number == 0:
        break

    # Convert the number to hexadecimal and binary
    hexadecimal = hex(number)
    binary = bin(number)

    # Display the results
    print(f"Hexadecimal: {hexadecimal}")
    print(f"Binary: {binary}")
    print()  # Blank line for better readability

 Output

Enter a number (0 to stop): 10
Hexadecimal: 0xa
Binary: 0b1010

Enter a number (0 to stop): 255
Hexadecimal: 0xff
Binary: 0b11111111

Enter a number (0 to stop): 0

 Share this Python Exercise

 More Python Programming Exercises of Python Data Types

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Binary Code

    This Python program prompts the user to enter a decimal number and displays its equivalent in binary form. Instead of using the str() function, the prog...

  •  Conditional Logic and Booleans

    This Python program uses the conditional operator to assign a boolean variable named "bothEven" the value "True" if both numbers entered by the user are...

  •  Exception Handling V2

    This Python program prompts the user for a real number and displays its square root. The program uses a "try...except" block to handle any potential err...

  •  Character

    This Python program prompts the user to input three letters and then displays them in reverse order. The program uses basic input and string manipulation techn...

  •  Triangular Shape

    This Python program prompts the user to input a symbol and a width, then displays a triangle of the specified width using that symbol for the inner part...

  •  String Password

    This Python program prompts the user for a username and a password (both should be strings) and repeats the process until the correct credentials are en...