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.

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 result.

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 is 1.75 but the result of the integer division is simply 1. 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 division operator /. 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 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.

(ch02_19_codelens)

Check your understanding

    data-7-1: What value is printed when the following statement executes?

    print(18 / 4)
    
  • 4.5
  • The / operator does exact division and returns a floating point result.
  • 5
  • The / operator does exact division and returns a floating point result.
  • 4
  • The / operator does exact division and returns a floating point result.
  • 2
  • The / operator does exact division and returns a floating point result.

    data-7-2: What value is printed when the following statement executes?

    print(18 // 4)
    
  • 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-3: What value is printed when the following statement executes?

    print(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.