Note: you do not need to completely understand the content of this section to continue learning about Python.
In lesson 36, I told you that some error messages or other unexpected results could occur if you tried with different numbers than the one I tried. Here are two such examples:
>>> 1,023
(1, 19)
>>> 1,099
File "<input>", line 1
1, 099
^
SyntaxError: invalid token
What's happening here is that Python interprets any number that start with a zero as being an octal number, that is a number expressed in base 8.
"Usual" numbers are expressed in base 10. They are made up of the 10 usual symbols: "0, 1, 2, 3, 4, 5, 6, 7, 8 and 9". A number like 345 is interpreted to mean:
345 = 3*100 + 4*10 + 5*1 or 345 = 3*10*10 + 4*10 + 5*1 or 345 = 3*10**2 + 4*10**1 + 5*10**0
[If the last one does not make any sense to you, it means that you still have to learn more mathematics in school! We have seen earlier what "**" means.] By comparison, a number in base 8 is made up of the following 8 symbols: "0, 1, 2, 3, 4, 5, 6 and 7". A number like "23" (or 023 in Python; that is "zero"23) in base 8 is meant to mean:
023 = 2*8 + 3*1 = 16 + 3 = 19 (in base 10)
This should explain the first result we showed above. The second comes from the fact that "9" is not an allowed symbol in base 8.
Confused?... Let's add to your confusion, in the hope of clarifying things! Python also knows about hexadecimal numbers (numbers in base 16). They are usually written as a combinations of the following 16 symbols: "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f". We indicate to Python that we are using an hexadecimal number by starting with the two characters "0x" (zero followed by x) like the following:
>>> 0x33 51 >>> 0xa 10 >>> 0xf 15 >>> 0x10 16 >>> 0x111 273 >>> 0x123 291
Let's see if we understand, starting with the last one:
0x123 = 1*16*16 + 2*16 + 3*1 = 256 + 32 + 3 = 291 (in base 10)
In base 16, "a" is the 10th symbol and "f" is the 15th. Make sure that you can verify that the results I have shown you above are correct!
Before we leave this lesson, and especially this section, I should say a few words about binary numbers, or numbers expressed in base 2. Binary numbers are made up of the following two symbols: "0 and 1". Here's a binary number and its translation in base 10:
101001 = 1*2**5 + 0*2**4 + 1*2**3 + 0*2**2 + 0*2**1 + 1*2**0 or 101001 = 1*32 + 1*8 + 1*1 = 41 (in base 10)
Binary numbers are very important ... no, they are essential for computers; the reason is fairly simple to understand, if we stick to a very basic description.
Think of a lightbulb and a light switch. If the light switch is on, the lightbulb gives light; that is because an electric current goes through it. If the light switch if off, the lightbulb does not give any light; no electric current goes through it. There are only two possibilities. The usual convention is the following: no current = 0; current goes through = 1.
Now, inside computers, we do not have lightbulbs, but there are miniature wires and switches [and other electronic components which are not important for our discussion]. The state of a computer at a certain instant can be given in terms of which wires (say the 3rd, the 5th, the 17th, ...) that have a current going through them. This could be represented as a series of 0s (wires with no current) and 1s (wires with current). This state corresponds to a certain number ... expressed in base 2! Computer programs essentially control the tiny switches that make currents go through the computer change with time.
Now, because 8 is a power of 2 (8=2*2*2), octal numbers are another useful representation of the natural numbers for computers. Similary for hexadecimal numbers (16=2*2*2*2). Because they are shorter (for us) to write, hexadecimal numbers are most often used, after decimal numbers, by programmers. You will likely get to use them when you start writing your own games using Python!