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
5 ** 2
(5 + 9) * (15 - 7)


The tokens +, -, and *, and the use of parentheses 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.

Remember that if we want to see the results of the computation, the program needs to specify that with a print statement. The first three computations occur, but their results are not printed out.

In Python 2.7, which we will be using, the division operator / produces a floating point result if either of the operands is of type float. If both are of type int, then it performs integer division, which truncates its result down to the next smallest integer.

Pay particular attention to the examples above. Note that it truncates, rather than rounding, so 9/5 is  and 5/9 is 0.

Take care that you choose the correct flavor of the division operator. If you want a truncated integer output, make both of your operands integers. If you want a floating point result, make one or both of your operands into floats.

If you want to be sure to get integer division, even on floating point operands, you can use the operator //.

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.

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.

Check your understanding

rec-5-45: What value is printed when the following statement executes?

print 18 / 4

• (A) 4.5
• Because 18 and 4 are ints, / does integer division.
• (B) 5
• Integer division results in truncation, not rounding up.
• (C) 4
• Because 18 and 4 are ints, / does integer division.
• (D) 4.0
• Because 18 and 4 are ints, / does integer division and produces an integer.
• (E) 2
• / does division. Perhaps you were thinking of %, which computes the remainder?

rec-5-46: What value is printed when the following statement executes?

print 18.0 / 4

• (A) 4.5
• Because 18.0 is a float, / does exact division.
• (B) 5
• Because 18.0 is a float, / does exact division.
• (C) 4
• Because 18.0 is a float, / does exact division.
• (D) 4.0
• Because 18.0 is a float, / does exact division.
• (E) 2
• / does division. Perhaps you were thinking of %, which computes the remainder?

rec-5-47: What value is printed when the following statement executes?

print 18.0 // 4

• (A) 4.5
• - The // operator does integer division, not exact divisions
• (B) 5
• - Integer division results in truncation, not rounding up.
• (C) 4
• - The // operator does integer division, but it yields a float when one of the operands is a float/
• (D) 4.0
• - The // operator does integer division, and it yields a float when one of the operands is a float.
• (E) 2
• - / does division. Perhaps you were thinking of %, which computes the remainder?

rec-5-48: What value is printed when the following statement executes?

print 18 % 4
`
• (A) 4.25
• The % operator returns the remainder after division.
• (B) 5
• The % operator returns the remainder after division.
• (C) 4
• The % operator returns the remainder after division.
• (D) 2
• The % operator returns the remainder after division.
Next Section - Function Calls