Skip to main content

Section 3.6 Operators and Operands

Expressions don’t compute values on their own-they rely on operators and operands to do the work. Operators are special tokens that represent computations like addition, multiplication and division. The values the operator works on are called operands.
An expression combines operands and operators to produce a single value. 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)
Python uses special characters called operators that perform the following operations:
There are several other operators that we will learn in future chapters. But these are the ones that you’re going to need more or less all of your assignments!
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 the word print. The first three computations occur, but their results are not printed out.
So the first six operators have functionality that you’re pretty familiar with! On the other hand, we have two more operators that are used frequently in programming but not that much in math classes. These are integer division, and modulo.
In the previous example 5 / 3 resulted 1.66666666 . In Python 3, which we will be using, the division operator / produces a floating point result (even if the result is an integer; 4/2 is 2.0). If you want truncated division, which ignores the remainder, you can use the // operator (for example, 5//2 is 2). Integer division, or the // operator, divides the number, but rounds it down to the nearest integer. So for the previous example, 5 // 3 will result 1
Pay particular attention to the examples above. Note that 9//5 truncates rather than rounding, so it produces the value 1 rather 2.
The truncated division operator, //, also works on floating point numbers. It truncates to the nearest integer, but still produces a floating point result. Thus 7.0 // 3.0 is 2.0.
When a variable name appears in place of an operand, python substitutes that variable name with its assigned value. For example, what if we wanted to convert 645 minutes into hours?
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.
The remainder operator is also 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

Checkpoint 3.6.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.

Checkpoint 3.6.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).

Checkpoint 3.6.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.
You have attempted of activities on this page.