Python Tutorial / Compare Values and Combine Boolean Conditions
Comparison operators test relationships between values, while logical operators combine conditions. Python’s and and or can return an operand rather than a bool, so separate truth testing from default-value selection.
Distinguish equality, assignment and identity
Use == for value equality, = for assignment and != for inequality. Chained comparisons such as 0 <= amount <= 10000 include both boundaries. Validate incoming data before assuming that a number and a numeric-looking string can be ordered together.
None represents the absence of a value and is normally checked with is None. The is operator tests object identity, whereas == tests equality. Two separate lists can have equal contents without being the same list object.
For the relevant APIs and platform requirements, see Python: stdtypes.
Turning logical expressions into branch rules build on the rules demonstrated in Choose Execution Paths with if, elif and else and Practice Conditions and Loops with a Score Summary.
Run comparisons and default expressions
Save the complete code below as conditions.py in a separate practice folder, using UTF-8. Run python conditions.py in a terminal with your project virtual environment selected. Outside a virtual environment on macOS or Ubuntu, use python3 when that is your interpreter command.
amount = 8000
category = "food"
print(0 < amount <= 10000)
print(amount > 5000 and category == "food")
print(not category)
print("" or "unknown")
print(0 or 100)
print([] == [], [] is [])
Expected output:
True True False unknown 100 True False
Understand truthiness and short-circuit evaluation
Empty strings, empty containers, zero and None are false in a condition. The nonempty string "False" is true, so bool("False") is unsuitable for parsing a textual configuration flag.
and does not evaluate its right side when the left side is false. or does not evaluate its right side when the left side is true. This short-circuit behavior can select an operand itself, which explains the unknown and 100 outputs rather than True or False.
Short-circuit evaluation controls whether the right operand runs. Neither False and check() nor True or check() calls check. Running print(False and print("called")) prints only False, not called. Do not hide required validation or storage work in an operand that may never execute.
Preserve valid zero and empty values
amount or 100 chooses 100 even when amount is a valid zero. If only None should trigger the default, write 100 if amount is None else amount. A shorter expression must not silently change what the data means.
- Try amounts 10000 and 10001 to inspect the inclusive boundary.
- Set category to an empty string and compare not category.
- Explain why list-content comparisons use == while an absence check uses is None.
Make the data policy explicit before combining conditions. Conditions that happen to give the expected answer for one positive value may fail at zero, an empty collection or a missing field.




