2.7. Operators and Operands¶
Operators are special tokens that represent computations like addition, multiplication and division. The values the operator works on are called operands.
The following are all legal Python expressions whose meaning is more or less clear:
20 + 32
hour - 1
hour * 60 + minute
minute / 60
5 ** 2
(5 + 9) * (15 - 7)
The tokens +
, -
, and *
, and the use of parenthesis for grouping,
mean in Python what they mean in mathematics. The asterisk (*
) is the
token for multiplication, and **
is the token for exponentiation.
Addition, subtraction, multiplication, and exponentiation all do what you
expect.
Note
The type of the values on either side of an operator, the operands, must be the same type. Also note that the type of the resulting number is the same type as the operands.
In general, you cannot perform mathematical operations on strings,
even if the strings look like numbers. The following are illegal
(assuming that message
has type string):
message - 1
"Hello" / 123
message * "Hello"
"15" + 2
Interestingly, the +
operator does work with strings, but for
strings, the +
operator represents concatenation, not
addition. Concatenation means joining the two operands by linking
them end-to-end. For example:
The output of this program is banana nut bread
. The space before the word
nut
is part of the string and is necessary to produce the space between
the concatenated strings. Take out the space and run it again.
In Python there are two different division operators, /
and //
.
The single slash, /
is for diviing two floating-point numbers and the
double slash, //
, is for dividing two integers.
There are two different division operators because they behave
slightly differently. The floating-point division operator, /
behaves just as you would expect. The integer division operator,
//
, however, always results in an integer value, even if it isn’t
the mathematically correct result. It always truncates its result
down to the next smallest integer (to the left on the number line).
Notice that the result of the above floating point division
is 2.5
but the result of the integer division is simply 2
.
When a variable name appears in the place of an operand, it is replaced with the value that it refers to before the operation is performed. For example, what if we wanted to convert 645 minutes into hours.
What if, on the other hand, we had wanted to know how many whole hours there are and how many minutes remain. To help answer this question, Python gives us a second flavor of the division operator. For example:
The modulus operator, sometimes also called the remainder
operator or integer remainder operator works on integers (and
integer expressions) and yields the remainder when the first operand
is divided by the second. In Python, the modulus operator is a percent
sign (%
). The syntax is the same as for other operators.
In the above example, 7 divided by 3 is 2 when we use integer division and there is a remainder of 1 when we use the modulus operator.
The modulus operator turns out to be surprisingly useful. For example,
you can check whether one number is divisible by another—if x %
y
is zero, then x
is divisible by y
. Also, you can extract
the right-most digit or digits from a number. For example, x % 10
yields the right-most digit of x
(in base 10). Similarly x %
100
yields the last two digits.
Finally, returning to our time example, the remainder operator is extremely useful for doing conversions, say from seconds, to hours, minutes and seconds. If we start with a number of seconds, say 7684, the following program uses integer division and remainder to convert to an easier form. Step through it to be sure you understand how the division and remainder operators are being used to compute the correct values.
Check your understanding
- 4.5
- The / operator does exact division and returns a floating point result.
- 5.0
- The / operator does exact division and returns a floating point result.
- 4.0
- The / operator does exact division and returns a floating point result.
- 2.0
- The / operator does exact division and returns a floating point result.
data-7-1: What value is printed when the following statement executes?
print(str(18.0 / 4.0))
- 4.25
- - The // operator does integer division and returns an integer result
- 5
- - The // operator does integer division and returns an integer result, but it truncates the result of the division. It does not round.
- 4
- - The // operator does integer division and returns the truncated integer result.
- 2
- - The // operator does integer division and returns the result of the division on an integer (not the remainder).
data-7-2: What value is printed when the following statement executes?
print(str(18 // 4))
- 4.25
- The % operator returns the remainder after division.
- 5
- The % operator returns the remainder after division.
- 4
- The % operator returns the remainder after division.
- 2
- The % operator returns the remainder after division.
data-7-3: What value is printed when the following statement executes?
print(str(18 % 4))