Although we usually use the decimal number, which is the one we learned in the very first years of our elementary school, sometimes we can come across other types of literals, like binary, octal or hexadecimal literals. You may need them one day, but even if you don’t, it doesn’t hurt to know what they are when you see them. So, let’s have a look at them now.
You may prefer to watch the video first. Here it is:
Table of Contents
Decimal Number
Let’s start with something familiar, decimal literals. Here the base is 10, so each digit the number consists of is a power of 10. There are 10 digits available: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
For example, the number 248 consists of three digits. Reading from right to left with increasing exponents we can say that the number’s value is equal to:
8 x 100 + 4 x 101 + 2 x 102 = 8 x 1 + 4 x 10 + 2 x 100 = 8 + 40 + 200 = 248
If you just type an integer number in Python code, it’s treated by default as a decimal number:
# decimal
>>> a = 21
>>> a
21
Now, let’s talk about the others:
Binary Number
In the binary system the base is 2, so each digit the number consists of is a power of 2. There are only 2 digits available: 0 and 1.
For example, the binary number 1011 consists of four digits. Reading from right to left with increasing exponents we can say that the number’s value is equal to:
1 x 20 + 1 x 21 + 0 x 22 + 1 x 23 = 1 x 1 + 1 x 2 + 0 x 4 + 1 x 8 = 1 + 2 + 0 + 8 = 11
In Python binary literals are prefixed by 0b or 0B:
# binary
>>> a = 0b1001
>>> a
9
>>> b = 0B110111001
>>> b
441
>>>
Octal Number
In the octal system the base is 8, so each digit the number consists of is a power of 8. There are 8 digits available: 0, 1, 2, 3, 4, 5, 6, 7.
For example, the octal number 217 consists of three digits. Reading from right to left with increasing exponents we can say that the number’s value is equal to:
7 x 80 + 1 x 81 + 2 x 82 = 7 x 1 + 1 x 8 + 2 x 64 = 7 + 8 + 128 = 143
In code octal literals are prefixed by 0o or 0O:
# octal
>>> a = 0o74524
>>> a
31060
>>> b = 0O1446
>>> b
806
>>>
Hexadecimal Number
In the hexadecimal system the base is 16, so each digit the number consists of is a power of 16. There are 16 digits available: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. The letter digits may be capital or small. Their values are from A = 10 to F = 15.
For example, the octal number 3B9 consists of three digits. Reading from right to left with increasing exponents we can say that the number’s value is equal to:
9 x 160 + 11 x 161 + 3 x 162 = 9 x 1 + 11 x 16 + 3 x 256 = 9 + 176 + 768 = 953
In code hexadecimal literals are prefixed by 0x or 0X:
# hexadecimal
>>> a = 0x16E2
>>> a
5858
>>> b = 0XF32
>>> b
3890
As mentioned before, in hexidecimal literals the letter digits may be capitalized or small:
# hexadecimal with small letter digits
>>> c = 0xcc1
>>> c
3265
>>>
Converting from Decimal Number to Binary, Octal and Hexadecimal
We can use the bin, oct and hex functions to convert a decimal literal into the corresponding string representation of its binary, octal or hexadecimal counterpart:
# the bin function
>>> bin(25)
'0b11001'
# the oct function
>>> a = 100
>>> oct(a)
'0o144'
# the hex function
>>> print("1000 in hexadecimal notation is " + hex(1000))
1000 in hexadecimal system is 0x3e8
Converting from Binary, Octal and Hexadecimal to Decimal Number
We can use the int function to get the decimal integer corresponding to the binary, octal or hexadecimal argument:
# the int function
>>> int(0b101)
5
>>> int(0o771)
505
>>> int(0x14a5)
5285