Python Coding Exercise #24: Binary, Octal and Hexadecimal
Part of the series Python Practice Exercise — part 24
Before you start: Know input() and int() · Know what a built-in function is
The question
Write a program that accepts an integer in decimal form and prints it in binary, octal, hexadecimal. Use built-in functions
bin,oct, andhex.
For example:
Input a number: 10
0b1010
0o12
0xa
My Solution
number = int(input("Input a number: "))
print(bin(number))
print(oct(number))
print(hex(number))
How My Solution Works
The program reads a whole number with int(input(...)) and then passes it to three built-in functions, which come with Python and need no import:
bin()converts it to binary.oct()converts it to octal.hex()converts it to hexadecimal.
Each one is printed on its own line.
What are these number systems?
The numbers we write every day use the decimal system, which has ten digits (0 to 9). Each position in a number is worth ten times the one to its right. A number system (or base) is just a different choice of how many digits to use:
| System | Base | Digits used | Used for |
|---|---|---|---|
| Decimal | 10 | 0 to 9 | everyday counting |
| Binary | 2 | 0 and 1 | how computers store everything |
| Octal | 8 | 0 to 7 | an older shorthand for binary |
| Hexadecimal | 16 | 0 to 9, then a to f | colours (like #ff0000), memory addresses |
Hexadecimal needs 16 digits, but we only have ten numerals, so it borrows letters: a is 10, b is 11, up to f, which is 15.
Why 10 becomes 1010
In binary, each position is worth double the one to its right: 8, 4, 2, 1, and so on. To write 10, you need 8 and 2, which is a 1 in the 8 and 2 positions and a 0 in the others:
| Position value | 8 | 4 | 2 | 1 |
|---|---|---|---|---|
| Digit | 1 | 0 | 1 | 0 |
That’s 1010, and 8 + 2 is 10. In octal, 10 is 12, which is one 8 plus two 1s. In hexadecimal, 10 is just the single digit a.
The prefixes: 0b, 0o, 0x
The results don’t look like plain numbers, because Python adds a prefix to show which system each one is in:
| Prefix | System | Example |
|---|---|---|
0b | binary | 0b1010 |
0o | octal | 0o12 |
0x | hexadecimal | 0xa |
So 0b1010 means “binary 1010”, which is 10. The prefix stops you from mixing up the numbers: without it, 10 could mean ten, two or eight.
Here are some more conversions:
| Decimal | bin() | oct() | hex() |
|---|---|---|---|
| 8 | 0b1000 | 0o10 | 0x8 |
| 10 | 0b1010 | 0o12 | 0xa |
| 16 | 0b10000 | 0o20 | 0x10 |
| 100 | 0b1100100 | 0o144 | 0x64 |
| 255 | 0b11111111 | 0o377 | 0xff |
The number 255 is a good one to remember. In binary, it’s eight 1s in a row, which is the biggest value that fits in one byte (8 bits). In hexadecimal it’s just ff, which is why hex is popular for colours and data.
Things to notice
The results are strings. bin(), oct() and hex() return text, not numbers. type(bin(10)) is <class 'str'>. So you can print them or slice them, but you can’t do arithmetic with them directly. All three are explained in Built-in Functions in Python.
Removing the prefix. If you only want the digits, you can slice off the first two characters, since the prefix is always two characters long:
print(bin(10)[2:]) # 1010
print(oct(10)[2:]) # 12
print(hex(255)[2:]) # ff
(Slicing is explained in Exercise #23.) An f-string can also do the conversion with no prefix, using the letters b, o and x after a colon:
number = 255
print(f"{number:b}") # 11111111
print(f"{number:o}") # 377
print(f"{number:x}") # ff
print(f"{number:X}") # FF (capital letters)
Going the other way
You can convert back to decimal with int(), by giving it the base as a second argument:
print(int("1010", 2)) # 10
print(int("12", 8)) # 10
print(int("ff", 16)) # 255
And in your own code you can write numbers in these systems directly, using the same prefixes:
print(0b1010) # 10
print(0o12) # 10
print(0xff) # 255
All three lines print decimal numbers, because Python always shows an integer in decimal unless you convert it to a string with bin(), oct() or hex().
Key takeaways
bin(),oct()andhex()convert an integer to binary, octal and hexadecimal.- The results are strings with a prefix:
0bfor binary,0ofor octal and0xfor hexadecimal. - Binary uses digits 0 and 1, octal uses 0 to 7, and hexadecimal uses 0 to 9 plus
atof. - Use slicing (
[2:]) or the f-string formats:b,:oand:xto get the digits without the prefix. int("ff", 16)converts a string in another base back into a decimal integer.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: This exercise is taken from Ultimate Python Programming: Learn Python with 650+ Programs, 900+ Practice Questions, and 5 Projects by Deepali Srivastava, published by BPB Publications. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.