Lecture 3 - Exercises

3A - Operators and Operator Precedence

  1. Evaluate each of the following expressions. Be sure to properly indicate the type of the value, for example by including a decimal point if the result is a float or enclosing it in double quotes if it’s a str.
    1. 9 / 3
    2. 9 // 3
    3. 10 // 3
    4. 10 % 3
    5. 3 % 10
    6. 2 ** 3
    7. "abc" + "def"
    8. "baa " * 2
  2. Evaluate each of the following expressions. As before, be sure to be precise about the type of the resulting value.
    1. (9 % ( 6 // 2))
    2. 9 % 6 // 2
    3. 2 ** 2 ** 4
    4. ("na" * 8 + " ") * 2 + "Batman!"
    5. 1 + 2 ** 3 / 4 * 5 - (6 % 7)

3B - Program Execution and Return Values

  1. For each of the following, say whether it is a statement or an expression:

    1. a = 4
    2. a + 4
    3. int(6.4) + 2
  2. Suppose we run the following program, and the user types 6 and presses enter. What happens?

    user_num = input("Enter a number: ")
    result = 5 % (3 ** (user_num // 4))
  3. Suppose we run the following program, and the user types 6 and presses enter. What value gets stored in result?

    user_num = int(input("Enter a number: "))
    result = 5 % (3 ** (user_num // 4))
  4. What does the following expression evaluate to? float(str(int(2.6 / 2))*2)