CPSC120A
Fundamentals of Computer Science I

Activity 2

Exploring the integer division and modulo operators.

Integer Division and Mod

Most of the operators that we use in the Python language are pretty self explanatory. You have been using + and * since elementary school. However, Python includes two additional operators that you might not be familiar with: // and %. In this activity you will experiment with these operators to see what their behaviors actually are.

Details

  1. Fill in the following table. For each row of the table, write a Python program that evaluates the expression on the left. Put the result in the column on the right.

    ExecuteResult
    5 / 3
    5 // 3
    5 % 3
    12 / 2
    12 // 2
    12 % 2
    23 / 11
    23 // 11
    23 % 11
  2. For the following table, what do you expect the result to be. Do not type these into the interpreter. Try to figure them out by hand.

    ExecuteResult
    7 / 2
    18 // 5
    11 % 2
    15 / 5
    16 // 3
    15 % 5
  3. Given the above results, what do you think the // operator does?

  4. Given the above results, what do you think the % operator does?

  5. Give an example of what these operators are useful for?

  6. The height of the tallest person in recorded history is 107 inches. Write a single print statement that will print the height of the tallest person in recorded history in feet and inches. Note the number of inches should be less than 12.