Text and numbers are different things
Why quotation marks change what something is, and the arithmetic you get for free.
Write the number twelve on a price tag, then write it in a sum. You are doing two different things. On the tag it is a label, a shape you could just as well have drawn as XII. In the sum it is a quantity: something you can double, halve, or add three to.
A computer keeps those two ideas strictly apart, and mixing them up is the first real confusion most people meet. The rule is short. Text goes inside quotation marks. Numbers do not.
The two lines differ only by a pair of quotation marks. Run it, then take the quotation marks off the second line and run it again.
Without quotation marks, 2 + 2 is a sum. Python works it out before printing anything, so what actually reaches print is 4. With quotation marks it is a piece of text that happens to contain two digits and a plus sign, and text is never worked out. It is carried through exactly as written.
A whole number such as 4 or -17 is called an integer. A number with a decimal point in it, such as 2.5, is called a float, short for floating point, which is just the computer's way of storing numbers that are not whole.
The arithmetic you get for free
Python already knows how to do arithmetic; you do not have to teach it. A symbol that does something to the things on either side of it is called an operator, and a value is any single thing the program is holding at that moment: the number 7, or the text "Hello". Six operators are worth knowing from the first day.
| Written | Means | Example | Result |
|---|---|---|---|
+ | add | 7 + 5 | 12 |
- | subtract | 7 - 5 | 2 |
* | multiply | 7 * 5 | 35 |
/ | divide | 7 / 2 | 3.5 |
// | divide and round down to a whole number | 7 // 2 | 3 |
% | what is left over after dividing | 7 % 2 | 1 |
Change the numbers and run it again. Notice that / gives 3.5 rather than 3. Try 8 / 2 as well: division always produces a number with a decimal point, even when it comes out even.
The last two operators in the table are a pair, and they are easier to remember as a picture than as a rule. Seventeen sweets are shared between five children.
The first line is how many sweets each child gets, the second is how many are left in the bag. Change 17 to 18 and run it again, then to 20.
Both of those behave slightly differently when a negative number is involved. You will meet that in the next section; everything here uses positive numbers.