Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
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)
*, 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
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.
In Python 3, division is denoted by the operator token
/ which always evaluates to a floating point
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. This version, called integer division, uses the token
//. It always truncates its result down to the next smallest integer (to
the left on the number line).
Pay particular attention to the first two examples above. Notice that the result of floating point division
1.75 but the result of the integer division is simply
Take care that you choose the correct flavor of the division operator. If
you’re working with expressions where you need floating point values, use the
/. If you want an integer result, use
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
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
Also, you can extract the right-most digit or digits from a number. For
x % 10 yields the right-most digit of
x (in base 10).
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