[Python] Chapter 02-06 <Supplement> Basics of Python programs (handling of numerical values)

[Python] Chapter 02-06 Handling of numerical values

Here, I would like to delve a little deeper into the handling of numerical values. Note that this section is rarely dealt with in practice and is </ font>, so you can skip this section.

It seems that the focus will be on a little theoretical story.

However, if you are considering taking the IT Passport Examination or the Fundamental Information Technology Engineer Examination, the content is covered by the basic theory, so please read it **.

About numerical values

As explained in "Chapter 02-01", the numerical values can be divided as follows.

--Integer
Example) 10, -7, 321 etc. --Floating point number (real number)
Example) 3.14, -2.718, 6.02 × 10 23 </ sup> etc.

In this, I would like to dig a little deeper into "integers".

Cardinal (N-ary)

The numbers we usually deal with are ** Decimal numbers **. The numbers are actually expressed using 10 types of numbers, "0", "1", "2", "3", "4", "5", "6", "7", "8", and "9".

However, the computer handles only two, "0" or "1". This is ** binary ( Binary number) **.

For example, the decimal number 45 is written as 101101 in the decimal number.

In addition to binary and decimal numbers, there are also octal numbers (Octal numbers) and hexadecimal numbers (Hexadecimal) as commonly used N-ary numbers.

Cardinal conversion

First of all, I would like to talk about the conversion between binary numbers and decimal numbers. (If you understand, you can skip it)

Conversion from binary to decimal

Consider converting the binary number 101101. I have summarized these binary numbers in the table below.

number 5 4 3 2 1 0
Binary number 1 0 1 1 0 1

Looking at the table above, each number is assigned a number. Moreover, I dared to assign 0, 1, 2, 3, 4, 5 from the right. Keep this number in mind as it will be important later.

Now, in the table above, we will add ** weights ** to each digit. The weight is N raised to the power of each digit, such as N m </ sup> for N-ary numbers. Please check the table below for a summary of what it means.

number 5 4 3 2 1 0
Binary number 1 0 1 1 0 1
weight 25 24 23 22 21 20

Then, multiply these weights by each digit of the binary number and add them as follows.

\begin{align}
&(2^{5}×1)+(2^{4}×0)+(2^{3}×1)+(2^{2}×1)+(2^{1}×0)+(2^{0}×1)\\
&=32  + 0 +  8 +  4 +  0 +  1\\
&=45
\end{align}

As mentioned above, I was able to find the decimal number.

Conversion from decimal number to binary number

Now, on the contrary, let's look at the conversion from decimal numbers to binary numbers. Let's change the decimal number 45 to a binary number.

In general, divide a decimal number by N in N-ary and find the remainder of each. This time it is converted to a binary number, so divide it by 2 as shown below. Dividing 45 by 2 gives 22 quotients, which is left over, so write it to the right.

45÷2=22 ・ ・ ・ 1

Furthermore, if you divide 22 by 2, the quotient becomes 11, which is divisible. In this case, consider ** 0 surplus ** as shown below.

22÷2=11 ・ ・ ・ 0

If you repeat this, it will be as follows. Often, to find a binary number, the reverse of division is often calculated.

1.png

At the end, there are too many "0" and "1" written on the right, but I will arrange them in order from the bottom.

2.png

Then, it became "101101", and I was able to find a decimal number of 45 decimal numbers.

Handling of hexadecimal and octal numbers

As mentioned above, in decimal numbers, numbers are expressed using 10 types of numbers: "0", "1", "2", "3", "4", "5", "6", "7", "8", and "9". ..

Eight numbers can be predicted to be expressed using eight types of numbers, "0", "1", "2", "3", "4", "5", "6", and "7", but hexadecimal numbers. What will happen?

After "9", "10", "11", "12", "13", "14", and "15" follow, but then "15" is really "15", "1" and "5" I don't know if they are stuck together.

Therefore, ** hexadecimal numbers are expressed using a to f ** as shown in the table below.

Binary number 8 base Decimal number Hexadecimal
0000 0 0 0
0001 1 1 1
0010 2 2 2
0011 3 3 3
0100 4 4 4
0101 5 5 5
0110 6 6 6
0111 7 7 7
1000 10 8 8
1001 11 9 9
1010 12 10 a
1011 13 11 b
1100 14 12 c
1101 15 13 d
1110 16 14 e
1111 17 15 f

Binary numbers are inevitably long when written, so expressing them in hexadecimal has the advantage of being able to compress the length and make it easier to see.

Conversion from hexadecimal to decimal

For example, consider representing the hexadecimal number "5c2" in decimal. This is also considered using a table. Since c is 12 from the above table, calculate with 12 as shown below.

number 2 1 0
Hexadecimal 5 c
(12)
2
weight 162 161 160

Add this with the same weight as in the case of binary numbers.

\begin{align}
&(16^{2}×5)+(16^{1}×12)+(16^{0}×2)\\
&=1280  + 192 +  2 \\
&=1474
\end{align}

On the contrary, the conversion from decimal number to hexadecimal number is also calculated by arranging the remainder divided by 16 as shown below.

3.png

Then, it became "5c2", and I was able to find the decimal number of 1474 decimal number.

In the case of octal numbers, divide by 8 and arrange the remainders in the same way.

Other N-ary numbers

So far, we have treated it as a binary number, an octadecimal number, a decimal number, and a hexadecimal number, but aren't we dealing with other binary numbers and pentadecimal numbers?

In fact, both ternary and quintuplet numbers exist, but they are rarely used. The method of finding it is the same as dividing the decimal number by 3.

In fact, binary numbers are compatible with octal and hexadecimal numbers, and can be obtained immediately as shown in the table above. As mentioned above, binary numbers have a long notation, so it is often used to convert them using hexadecimal numbers and compress them for easier viewing.

For example, if you want to convert the binary number "00101101" to a hexadecimal number as it is, you can convert the binary number by dividing it into four and find the hexadecimal number from the above table.

0010 1101 = 2d

Cardinal conversion in Python

Now, I would like to convert this using Python. This time, I will explain using ** Python Console **.

I will explain using the numerical values in the previous example. First, consider the conversion from binary numbers, octal numbers, and hexadecimal numbers to decimal numbers. Enter the code as shown below. From the top, it is converted to decimal numbers in the order of binary numbers, octal numbers, and hexadecimal numbers.

>>>bin(45)
'0b101101'
>>>oct(45)
'0o55'
>>>hex(1474)
'0x5c2'

You can use the ** bin function ** to convert to binary, the ** oct function ** to convert to octal, and the ** hex function ** to convert to hexadecimal. Regarding the output result, 0b (zero bee) represents Binary (binary number), 0o (zero o) represents Octal, and 0x (zero X) represents Hexadecimal (hexadecimal number).

Now, on the contrary, let's see how binary, octal, and hexadecimal numbers can be converted to decimal numbers. Enter the code as shown below.

>>>0b101101
45
>>>0o55
45
>>>0x5c2
1474

Just add "0b", "0o", and "0x" before the numbers you want to convert.

About floating point numbers

Finally, I will talk about floating point numbers as a supplement. For example, the following real numbers

2.718

Will be written as usual.

In addition, large numbers and conversely small numbers may be expressed using exponents. For example:

3.14 × 10^{3}\\
\\
5.1×10^{-2}

(*) </ font> The above 10 -2 </ sup> are as follows.

10^{-2}=\frac{1}{10^{2}}

If this is implemented in Python, it will be as follows.

>>>3.14e3
3140.0
>>>5.1e-2
0.051

Where e3 means 10 3 </ sup> and e-2 means 10 -2 </ sup>.

Floating-point numbers using exponents as described above are often ** used in scientific calculations **.

Practice problem

[1-1] Convert the following binary numbers to decimal numbers. (Please do it by hand) ・ 10001010 ・ 11111111 1010010111010 Check the results of [1-2] and [1-1] on the Python Console. [2-1] Convert the following hexadecimal numbers to decimal numbers. (Please do it by hand) ・ C3 ・ Ff ・ 475 Check the results of [2-2] and [2-1] on the Python Console. [3-1] Convert the following decimal numbers to binary numbers, octal numbers, and hexadecimal numbers. (Please do it by hand ・ 31 ・ 123 Check the results of [3-2] and [3-1] on the Python Console.

Return to [Table of Contents Link]

Recommended Posts