2.4. Operators and Operands¶
Operators are special tokens (usually symbols) that represent computations
like addition and multiplication. The values the operator is applied to are
called operands. For example, in the expression
5 + 10, the operator is
+, and the values
10 are operands.
- and parentheses (for grouping) in the same way they
are used in arithmetic.
Multiplication, division, and exponentiation
**, respectively. So
3 * 4 is “three times four”
3 ** 4 is “three raised to the fourth power” or \(3^4\).
The following code demonstrates the use of operators.
When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. For mathematical operators, Python follows mathematical convention:
Parentheses, Exponentiation, Multiplication and Division, Addition and Subtraction.
When in doubt, always put parentheses in your expressions to make sure the computations are performed in the order you intend. Even if it would work correctly without them, parentheses can help you and others understand complex expressions.
In Python 3, the division operation always evaluates to a float, even if the result is a whole number. For example:
See how the values are printed with decimal points even though the decimal part is zero? That is how Python indicates a value is a float.
So for example, if we wanted to convert a number of minutes, stored in a
minutes variable, into hours:
What if we just wanted to know how many whole hours are in 1234 minutes? Python provides a different division operator that can help. Integer division uses the token
//. It always rounds its result down to the nearest smaller integer (moving left on the number line):
The modulus operator works on integers and produces the remainder when
the first operand is divided by the second. In Python, the modulus operator is
a percent sign
Here, 7 divided by 3 is 2 (the quotient) with 1 left over (the remainder).
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. The following code finds numbers divisible by 9
(it uses a
for loop and other things we’ll learn about later, but you can
get an idea of how it works by reading the code and changing parts to see what
You can also get the right-most digit or digits from a number using modulus.
For example,``x % 10`` give you the right-most digit of
x (in base 10).
x %100 gives you the last two digits of
Let’s say we have a number of days, and we want to know how long that is in other units of time. For example, what is 17 days in hours? What is 17 days in weeks?
Converting to hours can be done with multiplication. If we set
days = 17, and then enter the code
hours = days * 24 the
computer would calculate our answer.
But what if we want something a bit more complicated, like
converting days into weeks plus days? For example, how could
we write a program where we enter
days = 17 and it then
2 weeks and 3 days?
See if you can figure out how the code below uses division and modulus to get the make this conversion.
2.4.4. String Operations¶
Most of the operations that work with numbers don’t make sense when applied to
strings. For example, as we’ve seen,
'Cat' / 'Dog' is an invalid
expression in Python. But two of the operators above are defined for strings,
though they do not perform arithmetic when applied to strings.
+ operator works with strings, but it is not addition in the
mathematical sense. Instead it performs concatenation, which means joining
the strings by linking them end to end. For example:
* operator also works with strings if applied to a string and an
integer. In this case, the operation is called string repetition.
2.4.5. Table of Operators¶
The following table summarizes the operators discussed above.
int, float Operation