You’ve now seen two types of numbers (integers and floating-point numbers), so we ought to explain what we mean by a type. In Python, a type consists of two things:
For example, in type int, the values are …, -3, -2, -1, 0, 1, 2, 3, …, and you have seen that these operators can be applied to those values: +, -, *, /, //, %, and **.
The values in type float are a subset of the real numbers, and it happens that the same set of operations can be applied to float values. You can observe the results of applying these to various values in the table below. If an operator behaves differently depending on the types of the operands, it is called an overloaded operator.
| Symbol | Operator | Example | Result |
|---|---|---|---|
| - | Negation | -5 | -5 |
| + | Addition | 11 + 3.1 | 14.1 |
| - | Subtraction | 5 - 19 | -14 |
| * | Multiplication | 8.5 * 4 | 34.0 |
| / | Division | 11 / 2 | 5.5 |
| // | Integer Division | 11 // 2 | 5 |
| % | Remainder | 8.5 % 3.5 | 1.5 |
| ** | Exponentiation | 2 ** 5 | 32 |
Floating-point numbers are not exactly the fractions you learned in grade school. For example, look at Python’s version of the fractions 2/3 and 5/3:
| | >>> 2 / 3 |
| | 0.6666666666666666 |
| | >>> 5 / 3 |
| | 1.6666666666666667 |
The first value ends with a 6, and the second with a 7, which is unexpected: both should have an infinite number of 6s after the decimal point. The problem is that computers have a finite amount of memory, and to perform calculations quickly and efficiently, most programming languages limit the amount of information that can be stored for any single number. The number 0.6666666666666666 turns out to be the closest value to 2/3 that the computer can store in its limited memory, and 1.6666666666666667 is as close as you can get to the real value of 5/3.
In Programming, a + b ≠ b + a ! | |
|---|---|
| | If you have to add up floating-point numbers, add them from smallest to largest to minimize the error. |
Let’s apply your knowledge of integers and floating-point numbers to convert Fahrenheit to Celsius. To do this, subtract 32 from the temperature in Fahrenheit and then multiply by 5/9:
| | >>> 212 - 32 * 5 / 9 |
| | 194.22222222222223 |
Higher-precedence operators are applied before lower-precedence operators
Python claims the result is 194.22222222222223 degrees Celsius, when in fact it should be 100. The problem is that multiplication and division have higher precedence than subtraction; in other words, when an expression contains a mix of operators, the * and / are evaluated before the - and +. This means that what you calculated was 212 - ((32 * 5) / 9): the subexpression 32 * 5 is evaluated before the division is applied, and that division is evaluated before the subtraction occurs.
You can alter the order of precedence by putting parentheses around subexpressions:
| | >>> (212 - 32) * 5 / 9 |
| | 100.0 |
This table shows the order of precedence for arithmetic operators.
| Precedence | Operator | Operation |
|---|---|---|
| Highest | ** | Exponentiation |
|
| - | Negation |
|
| *, /, //, % | Multiplication, division, integer division, and remainder |
| Lowest | +, - | Addition and subtraction |
Operators with higher precedence are applied before those with lower precedence. Here is an example that shows this:
| | >>> -2 ** 4 |
| | -16 |
| | >>> -(2 ** 4) |
| | -16 |
| | >>> (-2) ** 4 |
| | 16 |
Because exponentiation has higher precedence than negation, the subexpression 2 ** 4 is evaluated before negation is applied.
Operators on the same row of the table have equal precedence and are applied left to right, except for exponentiation, which is applied right to left. So, for example, because binary operators + and - are on the same row, 3 + 4 - 5 is equivalent to (3 + 4) - 5, and 3 - 4 + 5 is equivalent to (3 - 4) + 5.
It’s a good rule to parenthesize complicated expressions even when you don’t need to, since it helps the eye read things like 1 + 1.7 + 3.2 * 4.4 - 16 / 3. On the other hand, it’s a good rule not to use parentheses in simple expressions such as 3.1 * 5.