Skip to content
Foxipa
Code Completion 4 Challenges

Complete the Python Boolean operators code

Practice using short-circuit and/or expressions to select values, guard access, and avoid unnecessary calls.

Beginner ~8 min read

Challenge 1

Choose a default name only when username is empty.

python Your answer
username = ""
display_name = 
print(display_name)

Press Ctrl⌘Enter to check your answer

Challenge 2

Read the first item only when the list has an item; an empty list should not raise IndexError.

python Your answer
items = []
first = 
print(first)

Press Ctrl⌘Enter to check your answer

Challenge 3

Set message to the string "go" when ready is true, using and.

python Your answer
ready = True
message = 
print(message)

Press Ctrl⌘Enter to check your answer

Challenge 4

Return the cached value without calling load_saved() when a cached value exists.

python Your answer
calls = []

def load_saved():
    calls.append("loaded")
    return "fresh"

saved = "cached"
value = 
print(value)
print(calls)

Press Ctrl⌘Enter to check your answer