androidengineers.Book a session

Python 02 · Text and decisions

Comparisons, Boolean logic, and branching

articleSelf-paced

Learn the concept

A condition controls which branch executes. Comparisons produce Booleans; and, or, and not combine conditions. Boolean operations short-circuit, so the second operand may not be evaluated. Use that property carefully, not as a substitute for readable validation.

== compares values. is compares object identity and is appropriate for checks such as value is None, not for ordinary string equality. Empty containers and empty strings are false in a Boolean context. A missing value and a valid zero are not always the same business state.

Write the rule in plain language before coding it. A ticket should be escalated only when it is urgent and unresolved. Parentheses help make a compound rule clear. Avoid deeply nested branches when early validation or smaller functions can clarify the decision.

Run and inspect

def should_escalate(priority, resolved):
    return priority == "urgent" and not resolved

assert should_escalate("urgent", False)
assert not should_escalate("urgent", True)
value = 0
assert value is not None

Your exercise

Write a routing rule for normal, urgent, and unknown priorities. Test every combination of priority and resolved status.

Check your understanding

The unknown case has an explicit outcome. A zero value is not mistaken for a missing value.

YOUR LEARNING JOURNEY

0 of 119 available lessons completed

Progress saved in this browser. No account needed.
Comparisons, Boolean logic, and branching | Forward Deployed Engineer | Android Engineers