This article will walk you through the different numerical systems that can be used to represent numbers in Python.
The system that we usually use in our daily life is the decimal system and, as the name suggests, it is based on 10 basic digits from which we can obtain all the numbers.
Out of our ordinary life, other numerical systems are used, especially in the computer world. As you may already know, all computers are based on a binary system; however, sometimes even the octal (which has 8 basic digits) and the hexadecimal (based on 16 digits) systems are used for specific applications.
In the following sections you will see how to use some Python built-in functions to convert from one numerical system to the other.
Long Story Short
Python provides some built-in functions for converting numbers from one numerical system to another. More specifically, these functions allow converting integer numbers to the:
In addition to these, the function
float.hex() allows converting floating-point numbers from the decimal to the hexadecimal system.
The Decimal System
The Decimal system is the numerical system that we use the most in our ordinary life (no coincidence with the fact that we have precisely ten fingers); it is based on 10 different digits that are 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10, from which it is possible to obtain all the possible numbers that we need. Even if all the computer-based systems are based on the binary system (and later we will see why), the user interface of most programs like Python, uses the decimal system to store values and perform mathematical operations.
The Binary System
The binary system represents the foundation of all the computer-based systems that are in use nowadays; as the name suggest, it exploits two different digits, 1 and 0, to build up all the possible number combinations. The choice of the two digits simply derives from how a system like a computer ultimately works; if you think about it, all the tasks that your computer is able to perform can be linked to a series of ON/OFF signals from which the digits 1 and 0, respectively. In this way, just by using zeroes and ones, the binary system represents all the available numbers.
Do you know how the Binary to Decimal conversion works?
Converting from binary to decimal is pretty easy, this section will show you, using an example, how this can be done manually (if you are not interested, just skip it and go directly to the code part). Since the binary system is based on two digits, to convert a binary number into a decimal number we will have to exploit powers of 2. As an example, let’s see how to convert the binary number 11011 into its decimal representation.
First of all, each digit position will represent the power of two that will be used in the conversion:
1 1 0 1 1 24 23 22 21 20
At this point, each power of two should be multiplied by the respective binary digit (either 1 or 0) and summed with the others, in this case we get:
1 * 24 + 1 * 23 + 0 * 22 + 1 *21 + 1 *20 = 16 + 8 + 2 + 1 = 27
How to convert from Binary to Decimal and vice versa in Python
To convert from one numerical system to another in Python is extremely easy, we just have to remember which function should be used and what to pass as input parameter. To represent a binary number in Python, we have to use the prefix “0b” which tells the system that the digits that follows, actually represent a binary number. You can try this by printing the following code lines:
num = 10 num_bin = 0b10 print(num, num_bin) # 10 2
in the binary system, 10 represents the number 2; the output of the printing command on the variable
num_bin will be “2”; this is because Python, by default, prints the result using the decimal system. However, if we want to perform some mathematical operations, we have to first convert the number into a decimal number and this can be done by using the function
bin_to_dec = int(0b10) print(bin_to_dec) # 2
On the other hand, to convert a decimal number to the binary notation, the appropriate function is
bin(), you can try to check personally if the result of the following code lines is correct.
dec_to_bin = bin(2234) print(dec_to_bin) # 0b100010111010
The Octal System
As suggested by the name, the Octal system is based on eight basic digits, from 0 to 7. A curious fact is that some Native Americans tribes adopt the Octal system because they count using the spaces between the fingers or the knuckles of their closed fists (if you want to know more about this: Yuki language (spaces between fingers), Pamean languages (knuckles of closed fists)). The procedure used to convert a number in the Octal system is similar to the one used above. In Python, octal numbers are identified by the prefix “0o” and the function that is used to do the conversion is called
oct(); the input, of course, is the decimal number that we want to convert.
dec_to_oct = oct(57) print(dec_to_oct) # 0o71
The opposite operation can still be performed by using again the
oct_to_dec = int(0o71) print(oct_to_dec) # 57
As you may imagine, it is also possible to convert directly from binary to octal and vice versa. The functions at play are always the same, let’s see how to convert the binary 0b100010111010 in the octal system:
bin_to_oct = oct(0b100010111010) print(bin_to_oct) # 0o4272
Do you remember which was the equivalent for this number in the decimal notation? We can use Python to get the answer instantaneously; however, the results that we get from functions like
oct() are expressed in the string format, this means that if we enter them as input parameters of the function
int(), we get an error, stating that Python cannot convert a string into a number. To solve this issue we can specify the format of the input parameter in the following way:
oct_to_dec = int(bin_to_oct, 8) print(bin_to_oct, oct_to_dec) # 0o4272 2234
In the binary case, we would have used “2”.
The Hexadecimal System
The Hexadecimal system is based on 16 digits, since we only have 9 different numbers in our decimal system, also letters are employed, namely:
The conversion to the hexadecimal system is analogous to the one illustrated for the octal system, the only difference is that this time the powers will have base 16. In this way, 17 becomes 11 in the hexadecimal notation. To denote a hexadecimal number in Python, we have to use the prefix “0x”. The function that allows us converting from one numeric system to the hexadecimal one is called
hex() and accepts as input parameter any integer number expressed in one of the aforementioned numeric systems.
dec_to_hex = hex(2234) print(dec_to_hex) # 0x8ba bin_to_hex = hex(0b100010111010) print(bin_to_hex) # 0x8ba oct_to_hex = hex(0o4272) print(oct_to_hex) # 0x8ba
What about floating numbers?
As you may have noticed, all the mentioned functions do accept as input for the conversion only integer numbers; this is because the expression of floating-point numbers is not unique, there are different notations which could be dependent on the computing system that we are using.
However, for the case of Hexadecimal numbers, Python provides a built-in function to convert floating numbers from the decimal system to the hexadecimal system. The function is called
float.hex() and accepts as input parameter the floating number in the decimal notation. The following code lines provide an example of its use.
float_dec_to_hex = float.hex(416.8) print(float_dec_to_hex) # 0x1.a0ccccccccccdp+8
As you can see, the notation that is used to express the answer is quite different from the one used for the integer numbers; this is because in these cases Python uses the scientific notation. The letter “p” stands for “times 2 to the power of” and “+8” is the exponent, while the bullet is then the decimal point. This is just one of the possible notation for expressing floating-point numbers in different notations; if you are interested in how it works, here are some useful links:
- i) https://en.wikipedia.org/wiki/Hexadecimal,
- ii) https://stackoverflow.com/questions/54947861/32-bit-ieee-754-single-precision-floating-point-to-hexadecimal .
In the article, we took a tour through the most widely used numerical systems and the functions that Python offers for converting from one notation to the other. When programming, it is good practice to be able at least to recognize the employed numerical system, in order to decide which of the above-mentioned functions to use for converting into the numerical system that is currently in use.