In the last lesson, we saw how we could teach Reeborg to add two numbers. In this lesson, we will see how Python goes about adding numbers and more.
Select the Python interpreter (Python: Code and Learn), by clicking on the third tab. Your display should look something like the following (other than having French used as the default language for the GUI):
Ignore the first four lines of the display for now. On the fifth line is the Python prompt
Put the cursor on line 5, click to select it and enter "8+4" followed by the "enter" key. You should see something like this on the display:
>>> 8+4 12 >>>
Surprise! Python added the two numbers right away (which was so difficult to get Reeborg to do). It's even prompting you to give it more instruction. You can try something like the following, which we basically "cut and pasted" from the interpreter to here.
>>> 8+4 12 >>> 8-4 4 >>> 8*4 32 >>> 8/4 2.0
As you can see, Python knows how to add, subtract, multiply and divide. This last case deserves an explanation. Please, take the time to read it all, it's not that complicated.
Instead of giving the integer "2" as the result of "8/4", Python gave us "2.0". This is not the "normal" result for a division with Python. "Normally", when you ask Python to divide one integer by another, the result is also an integer. This means that Python would "normally" give you
8/4 = 2
... but it would also give 8/3 = 2, that is to say that, as you remembered when you first learned about division, "8 divided by 3 is equal to 2 with a remainder of 2." In other words, 8/3 is two and two-thirds (2 and 2/3). Actually, when you first learned about (integer) division, you were probably told that you could not divide 8 by 3. Later, you learned about remainders in division. Even later, you learned about decimal numbers.
"Normally", Python deals with integer divisions the way you first learned about it. This leads to funny situations such as where Python would give 1/2 = 0 (zero). However, if you ask Python to divide two decimal numbers (or one decimal and one integer number), you would get the expected result, such as 1./2 = 0.5
Future versions of Python will work differently. What I did for you is to import the future version of "division", as displayed in the fourth line of the interpreter. If you want to do integer division, use a double division sign as follows:
>>>8//3 2 >>> 8.0//3. 2.0
Don't take just my word for it; try it yourself!
The double division sign is also known as the "floor" division. It takes the result of the division, and rounds it down to the nearest integer. If one divides two integers together, the result is an integer. If one of the numbers (or both) is a decimal number, the result is also a decimal number (rounded down to the nearest integer).
Python knows about the order of precedence of mathematical operators. By this we mean that, when you have many numbers and mathematical operations, you multiply and divide numbers first (from left to right), then add and subtract, etc. Here, a few examples are probably a better explanation than words could provide on their own.
>>> 2+3*5 17 >>> 2+ (3*5) # Python does not care about spaces here 17 >>> (2+3)*5 25 >>> 2*4/8 1.0 >>> 2*4//8 1 >>> 2+1-4 -1
Go ahead, try some more examples on your own!
Python knows more than the basic mathematical operations. Here are some more examples for you to try to expand on.
>>> 3*3*3*3 81 >>> 3**4 # exponentiation 81 >>> 7%3 # remainder: 7/3 = 2 with a remainder of 1 1 >>> 7.0 % 3 1.0 >>> 7.25 % 3 1.25 >>> 7.3 % 3 1.2999999999999998
This last result is almost equal to 1.3 which is what we might have expected. The difference between 1.3 and 1.2999999999999998 is tiny ... and is caused by the way computers work with decimal fractions. I will explain how this comes about in a later lesson, when you know more about computer programming in general. Just note that, in practice, such small difference between the "exact" result and that given by Python should not matter.
Try the following:
>>> n = 2147483646 >>> n+1 2147483647 >>> n+2 2147483648L
We have used "n" as a "synonym" for the number 2147483646. Remember our discussion of synonyms when we talk of translating move into avance for French readers. Python supports "synonyms" for things other than robot commands. In computer jargon, we talk of "variables" instead of "synonyms". So we would say that "n" is a variable whose value is 2147483646.
The result of n+1 is what we would expect, but the one for n+2 has a curious letter "L" tagged on the end. This is to denote what is known as a Long integer, that is to say an integer which can not be easily represented in computer memory. For a computer with a so-called 32-bit chip, such as the one I, and most likely you are using, all positive numbers are represented by various combinations of 31 "bits", and the largest integer that can be thus represented is -1 = 2147483647. Any integer larger than this requires Python (or any other computer language) to use advanced technique to perform mathematical operations on these numbers. This slows down computations; Python reminds us of this by tagging the letter L at the end. Unless you really need it, try to avoid using extremely large numbers!
You are most likely familiar with the scientific notation:
As you know, this notation allows us to write extremely small or extremely large numbers in a very convenient way. Python also knows the scientific notation but uses the letter E (or e) to represent x 10
>>> 2e3 2000.0 >>> 2.5E-1 0.25
Python also knows how to manipulate complex numbers as well as octal (base 8) and hexadecimal (base 16) numbers. Do not worry if you don't know what these are yet; we will explain what they are when we need them later.