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.

Note

The type of an expression’s result is the same type as the numbers in the expression. Add two integers together and the result is an integer. Add two floats together and the result is a float

The type of the values on either side of an operator, the operands, must be the same type. It is an error if the two operands are different types:

print(2 + 3.0) # Error: operands must be the same type

In general, you cannot perform mathematical operations on strings, even if the strings look like numbers. The following are illegal:

"Hello" - "o"
"Hi" * "Bye"
"14" / "2"

Interestingly, the + operator does work with strings, but for strings, the + operator represents concatenation, not addition. Concatenation means joining the two operands by linking them end-to-end. For example:

The output of this program is banana nut bread. The space before the word nut is part of the string and is necessary to produce the space between the concatenated strings. Take out the space and run it again to see what happens if it is missing.

In Python there are two different division operators, / and //. The single slash, / is for dividing two floating-point numbers. The double slash, //, is for dividing two integers.

There are two different division operators because they behave slightly differently. The floating-point division operator, / behaves just as you would expect. The integer division operator, //, however, always results in an integer value, even if it isn’t the mathematically correct result. It always truncates its result down to the next smallest integer (to the left on the number line).

Notice that the result of the above floating point division is 2.5 but the result of the integer division is simply 2.

Integer division is useful when performing calculations on whole objects. For example, if we want to convert minutes to whole hours:

The modulus operator, sometimes also called the remainder operator or integer remainder operator works on integers and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign (%):

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. Use CodeLens to 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

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

    print(18.0 / 4.0)
    
  • 4.5
  • The / operator does exact division and returns a floating point result.
  • 5.0
  • The / operator does exact division and returns a floating point result.
  • 4.0
  • The / operator does exact division and returns a floating point result.
  • 2.0
  • 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.
You have attempted of activities on this page
2.6. Expressions"> 2.8. Input">Next Section - 2.8. Input