In this lessons you'll learn about three number systems: Binary (base-2), Decimal (base-10), and Hexadecimal (base-16).
Binary is used by computers because it represents data using only 0s and 1s. Decimal is the system we use daily, while hexadecimal is often used in programming to represent large binary numbers compactly.
Read the following examples:
Let's take a look at binary numbers, which are the foundation of how computers store and process data. Binary numbers use only two digits: 0 and 1. This is called base-2.
Each digit in a binary number is called a 'bit'. The position of each bit represents a power of 2, starting from 20 on the far right. Here's an example:
| Binary Number | Position (Power of 2) | Value |
|---|---|---|
| 1 | 23 | 8 |
| 0 | 22 | 0 |
| 1 | 21 | 2 |
| 0 | 20 | 0 |
Adding these values together gives: 8 + 0 + 2 + 0 = 10 (in decimal).
Binary numbers are used because computers operate using electrical signals, which can either be on (1) or off (0). This makes binary the most efficient way for computers to represent data.
Now that you understand binary numbers, you're ready to explore how to convert between binary and other number systems!
In this step, you'll learn about decimal numbers, which are the number system we use in our everyday lives. Decimal numbers are based on base-10, meaning they use ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
Each digit in a decimal number has a position, and each position represents a power of 10, starting from 100 on the far right. Here's an example:
| Decimal Number | Position (Power of 10) | Value |
|---|---|---|
| 3 | 102 | 300 |
| 4 | 101 | 40 |
| 5 | 100 | 5 |
Adding these values together gives: 300 + 40 + 5 = 345.
Decimal numbers are widely used because they are intuitive and align with the way humans have historically counted using ten fingers. However, computers use binary numbers instead, as they are more efficient for electronic systems. Understanding decimal numbers is essential for converting between different number systems, which you'll explore in this lesson!
Next let's learn about hexadecimal numbers, which are based on base-16. Hexadecimal numbers use sixteen symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. The letters A to F represent the decimal values 10 to 15.
Each digit in a hexadecimal number has a position, and each position represents a power of 16, starting from 160 on the far right. Here's an example:
| Hexadecimal Number | Position (Power of 16) | Value |
|---|---|---|
| 1 | 162 | 256 |
| A | 161 | 160 |
| F | 160 | 15 |
Adding these values together gives: 256 + 160 + 15 = 431 (in decimal).
Hexadecimal numbers are widely used in computing because they provide a more compact way to represent binary numbers. For example, a single hexadecimal digit can represent four binary digits (bits). This makes hexadecimal especially useful for representing memory addresses, colours in web design, and other binary data in a human-readable format.
Now that you understand hexadecimal numbers, you're ready to explore how to convert between hexadecimal and other number systems!
In this step, you'll write Python code to convert a decimal number to binary.
Add the following code:
# Function to convert decimal to binary
def decimal_to_binary(decimal_number):
return bin(decimal_number).replace("0b", "")
# Test the function
decimal_number = 10
binary_result = decimal_to_binary(decimal_number)
print(f"The binary representation of {decimal_number} is {binary_result}") Run your code. You should see the binary representation of the decimal number 10. Try changing the value of decimal_number to test with other numbers!
Now lets write some Python code to convert a binary number to decimal.
Add the following code:
# Function to convert binary to decimal
def binary_to_decimal(binary_number):
return int(binary_number, 2)
# Test the function
binary_number = "1010"
decimal_result = binary_to_decimal(binary_number)
print(f"The decimal representation of {binary_number} is {decimal_result}") Run your code. You should see the decimal representation of the binary number 1010. Try changing the value of binary_number to test with other binary numbers!
In this step, you'll write Python code to convert a decimal number to hexadecimal.
Add the following code:
# Function to convert decimal to hexadecimal
def decimal_to_hexadecimal(decimal_number):
return hex(decimal_number).replace("0x", "").upper()
# Test the function
decimal_number = 255
hexadecimal_result = decimal_to_hexadecimal(decimal_number)
print(f"The hexadecimal representation of {decimal_number} is {hexadecimal_result}") Run your code. You should see the hexadecimal representation of the decimal number 255. Try changing the value of decimal_number to test with other numbers!
Next lets write some Python code to convert a hexadecimal number to decimal.
Add the following code:
# Function to convert hexadecimal to decimal
def hexadecimal_to_decimal(hexadecimal_number):
return int(hexadecimal_number, 16)
# Test the function
hexadecimal_number = "FF"
decimal_result = hexadecimal_to_decimal(hexadecimal_number)
print(f"The decimal representation of {hexadecimal_number} is {decimal_result}") Run your code. You should see the decimal representation of the hexadecimal number FF. Try changing the value of hexadecimal_number to test with other hexadecimal numbers!
Now lets write some Python code to convert a binary number to hexadecimal.
Add the following code:
# Function to convert binary to hexadecimal
def binary_to_hexadecimal(binary_number):
decimal_number = int(binary_number, 2)
return hex(decimal_number).replace("0x", "").upper()
# Test the function
binary_number = "11111111"
hexadecimal_result = binary_to_hexadecimal(binary_number)
print(f"The hexadecimal representation of {binary_number} is {hexadecimal_result}") Run your code. You should see the hexadecimal representation of the binary number 11111111. Try changing the value of binary_number to test with other binary numbers!
Finally, you'll write Python code to convert a hexadecimal number to binary.
Add the following code:
# Function to convert hexadecimal to binary
def hexadecimal_to_binary(hexadecimal_number):
decimal_number = int(hexadecimal_number, 16)
return bin(decimal_number).replace("0b", "")
# Test the function
hexadecimal_number = "FF"
binary_result = hexadecimal_to_binary(hexadecimal_number)
print(f"The binary representation of {hexadecimal_number} is {binary_result}") Run your code. You should see the binary representation of the hexadecimal number FF. Try changing the value of hexadecimal_number to test with other hexadecimal numbers!
Now, it's time to bring everything together! You'll build a Python program that allows a user to input a number and choose the conversion they want (e.g., decimal to binary, binary to hexadecimal, etc.).
Add the following code:
# Comprehensive conversion tool
def conversion_tool():
print("Choose a conversion type:")
print("1: Decimal to Binary")
print("2: Binary to Decimal")
print("3: Decimal to Hexadecimal")
print("4: Hexadecimal to Decimal")
print("5: Binary to Hexadecimal")
print("6: Hexadecimal to Binary")
choice = int(input("Enter your choice (1-6): "))
if choice == 1:
decimal_number = int(input("Enter a decimal number: "))
print("Binary:", decimal_to_binary(decimal_number))
elif choice == 2:
binary_number = input("Enter a binary number: ")
print("Decimal:", binary_to_decimal(binary_number))
elif choice == 3:
decimal_number = int(input("Enter a decimal number: "))
print("Hexadecimal:", decimal_to_hexadecimal(decimal_number))
elif choice == 4:
hexadecimal_number = input("Enter a hexadecimal number: ")
print("Decimal:", hexadecimal_to_decimal(hexadecimal_number))
elif choice == 5:
binary_number = input("Enter a binary number: ")
print("Hexadecimal:", binary_to_hexadecimal(binary_number))
elif choice == 6:
hexadecimal_number = input("Enter a hexadecimal number: ")
print("Binary:", hexadecimal_to_binary(hexadecimal_number))
else:
print("Invalid choice!")
# Run the tool
conversion_tool() Run your program and test all the options. You've just created a powerful conversion tool!
Every lesson in the course is ready to teach like this one. See what else comes with it, and how to order it.