Skip to content
Foxipa
Code Completion 4 Challenges

Complete the Python operator precedence code

Practice grouping expressions explicitly, applying operator precedence, and reasoning about evaluation order.

Intermediate ~9 min read

Challenge 1

Add parentheses so the addition happens before multiplication.

python Your answer
result =  * 4
print(result)

Press Ctrl⌘Enter to check your answer

Challenge 2

Make -2 the base of the exponentiation.

python Your answer
result =  ** 2
print(result)

Press Ctrl⌘Enter to check your answer

Challenge 3

Make the denominator `5 * 2` so the division happens after the multiplication.

python Your answer
result = 20 / 
print(result)

Press Ctrl⌘Enter to check your answer

Challenge 4

Place the two function calls as left and right operands so the recorded order is left, right.

python Your answer
events = []

def mark(name, value):
    events.append(name)
    return value

result = mark("", 1) + mark("right", 2)
print(result)
print(events)

Press Ctrl⌘Enter to check your answer