These questions follow Data in Programs. Operators are the verbs of a program — and Python applies them in a strict order, not yours.
Questions
- Evaluate
2 + 3 * 4— and explain why the answer is not20. - Predict the output of
print(7 // 2, 7 % 2). - Predict the output of
print("na" * 4 + " Batman!"). - Trace
x, then predict what prints — and what type of value it is.x = 10 x = x + 2 * 3 print(x > 15) - With
age = 16, evaluate each: (a)age >= 13 and age <= 19, (b)not (age == 16), (c)age == 16 or age == 61. - Find the bug.
if answer == "yes" or "y":runs its branch no matter what the user typed. Why — and what should it say? - Challenge. Add one pair of brackets to
2 + 3 * 4 - 1so it equals19— then a different pair so it equals11.
Answers
Answer 1
14. Multiplication happens before addition — the order of operations from maths class survives intact in Python.
Answer 2
3 1—//is division that keeps only the whole part, and%hands back the remainder. Seven is two twos with one left over.
Answer 3
nananana Batman!—*repeats the string four times first, then+glues on the ending. Text has operators too.
Answer 4
Multiply first:
xbecomes10 + 6, so16. Then16 > 15isTrue— a Boolean (bool), the yes/no type conditionals live on.
Answer 5
(a)
True— both sides hold. (b)False—notflips a truth. (c)True—orneeds only one side, and the first delivers.
Answer 6
Python reads it as
(answer == "yes") or ("y")— and a non-empty string like"y"counts as true on its own, every single time. It should sayanswer == "yes" or answer == "y".
Answer 7
(2 + 3) * 4 - 1gives19;2 + 3 * (4 - 1)gives11. Brackets outrank everything — same symbols, three different 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