These questions follow Data in Programs. Operators are the verbs of a program — and Python applies them in a strict order, not yours.

Questions

  1. Evaluate 2 + 3 * 4 — and explain why the answer is not 20.
  2. Predict the output of print(7 // 2, 7 % 2).
  3. Predict the output of print("na" * 4 + " Batman!").
  4. Trace x, then predict what prints — and what type of value it is.
    x = 10
    x = x + 2 * 3
    print(x > 15)
  5. With age = 16, evaluate each: (a) age >= 13 and age <= 19, (b) not (age == 16), (c) age == 16 or age == 61.
  6. Find the bug. if answer == "yes" or "y": runs its branch no matter what the user typed. Why — and what should it say?
  7. Challenge. Add one pair of brackets to 2 + 3 * 4 - 1 so it equals 19 — then a different pair so it equals 11.

Answers

Curriculum connection

C1.3

identify various types of data and explain how they are used within programs

Link to original

C1.4

determine the appropriate expressions and instructions to use in a programming statement, taking into account the order of operations

Link to original

C2.5

write programs that include the use of Boolean operators, comparison operators, text operators, and arithmetic operators

Link to original