You’re familiar with mathematical expressions like 3 + 4 (“three plus four”) and 2 - 3 / 5 (“two minus three divided by five”); each expression is built out of values like 2, 3, and 5 and operators like + and -, which combine their operands in different ways. In the expression 4 / 5, the operator is “/” and the operands are 4 and 5.
Expressions don’t have to involve an operator: a number by itself is an expression. For example, 212 is an expression as well as a value.
Like any programming language, Python can evaluate basic mathematical expressions. For example, the following expression adds 4 and 13:
| | >>> 4 + 13 |
| | 17 |
The >>> symbol is called a prompt. When you start IDLE, the window should open with this symbol displayed; you don’t type it. It is prompting you to type something. Type 4 + 13, and then press the Return (or Enter) key to signal that you are done entering that expression. Python then evaluates the expression.
When an expression is evaluated, it produces a single value. In the previous expression, the evaluation of 4 + 13 produced the value 17. When you type the expression in the shell, Python shows the value that is produced.
Subtraction and multiplication are similarly unsurprising:
| | >>> 15 - 3 |
| | 12 |
| | >>> 4 * 7 |
| | 28 |
The following expression divides 5 by 2:
| | >>> 5 / 2 |
| | 2.5 |
The result has a decimal point, even if it is a whole number:
| | >>> 4 / 2 |
| | 2.0 |
Every value in Python has a specific type
Every value in Python has a particular type (a data type, and the types of values determine how they behave when they’re combined. Values like 4 and 17 have type int (short for integer), and values like 2.5 and 17.0 have type float. The word float is short for floating point, which refers to the decimal point that moves around between digits of a number.
An expression involving two floats produces a float:
| | >>> 17.0 - 10.0 |
| | 7.0 |
When an expression’s operands are an int and a float, Python automatically converts the int to a float. This conversion is why the following two expressions both return the same answer:
| | >>> 17.0 - 10 |
| | 7.0 |
| | >>> 17 - 10.0 |
| | 7.0 |
If you want, you can omit the zero after the decimal point when writing a floating-point number:
| | >>> 17 - 10. |
| | 7.0 |
| | >>> 17. - 10 |
| | 7.0 |
However, this omission is considered bad style since it makes your programs harder to read; it’s very easy to miss a dot on the screen and see 17 instead of 17. (with a period).
Now and then, you want only the integer part of a division result. For example, you might want to know how many 24-hour days there are in 53 hours (which is two 24-hour days plus another 5 hours). To calculate the number of days, you can use integer division:
| | >>> 53 // 24 |
| | 2 |
You can determine the number of hours remaining by using the modulo operator, which returns the remainder of the division:
| | >>> 53 % 24 |
| | 5 |
Python doesn’t round the result of integer division. Instead, it takes the floor of the result of the division (truncates the fractional part):
| | >>> 17 // 10 |
| | 1 |
Be careful about using % and // with negative operands. Because Python takes the floor of the result of an integer division, the result is one smaller than you might expect if the result is negative:
| | >>> -17 // 10 |
| | -2 |
When using modulo, the sign of the result matches the sign of the divisor (the second operand):
| | >>> -17 % 10 |
| | 3 |
| | >>> 17 % -10 |
| | -3 |
For the mathematically inclined, the relationship between // and % comes from this equation: for any two non-zero numbers a and b, (b * (a // b) + a % b) is equal to a.
For example, because -17 // 10 is -2, and -17 % 10 is 3; then 10 * (-17 // 10) + -17 % 10 is the same as 10 * -2 + 3, which is -17.
Floating-point numbers can also be operands for // and % operators. With //, division is performed, and the result is rounded down to the nearest whole number, although the type is a floating-point number:
| | >>> 3.3 // 1 |
| | 3.0 |
| | >>> 3 // 1.0 |
| | 3.0 |
| | >>> 3 // 1.1 |
| | 2.0 |
| | >>> 3.5 // 1.1 |
| | 3.0 |
| | >>> 3.5 // 1.3 |
| | 2.0 |
The following expression calculates 3 raised to the power of 6:
| | >>> 3 ** 6 |
| | 729 |
Operators that have two operands are referred to as binary operators. Negation is a unary operator because it applies to one operand:
| | >>> -5 |
| | -5 |
| | >>> --5 |
| | 5 |
| | >>> ---5 |
| | -5 |