4.1.1.2, 4.1.1.4, 4.1.1.5 - Selection and Boolean conditions (Python)
Build precise comparisons, combine Boolean conditions, and choose the correct branch of a Python program, including boundary and nested cases.
Relational comparisons and boundaries
A relational comparison compares values and produces a Boolean result. Python uses ==, !=, <, >, <= and >= for equal, not equal, less, greater, less or equal, and greater or equal. Assignment = changes a binding; comparison == checks equality.
score = 50
print(score == 50, score != 50)
print(score < 50, score > 50)
print(score <= 50, score >= 50)
The output is True False, False False, True True. The difference between < and <= matters at the boundary. To accept an age from 16 through 18 inclusive, use 16 <= age <= 18, or age >= 16 and age <= 18. Tests at 15, 16, 18 and 19 check both sides and both accepted endpoints.
Compare like meanings: input returns text, so int(input()) is required for numerical age comparisons. Text comparison is lexicographic, not numerical; "10" < "2" is true because the first differing character is compared. Use meaningful names and stated units, such as height_cm, so height_cm >= 140 is interpretable.
Boolean operators and compound conditions
Boolean operators combine true/false decisions into a larger decision. not reverses a Boolean; and requires both conditions; or requires at least one. Exclusive OR (XOR) is true when exactly one of two Boolean conditions is true. In Python, a != b expresses XOR when a and b are Booleans; a ^ b also works for Booleans, but ^ on integers is bitwise XOR, not a general replacement for or.
| a | b | a and b | a or b | a != b |
|---|---|---|---|---|
| False | False | False | False | False |
| False | True | False | True | True |
| True | False | False | True | True |
| True | True | True | True | False |
not binds before and, which binds before or; parentheses clarify intended grouping. member and (weekday or holiday) requires membership in both cases. member and weekday or holiday instead allows any holiday visitor, even without membership.
Python's and and or short-circuit: the second expression is evaluated only when needed. denominator != 0 and numerator / denominator > 2 avoids division by zero because the first condition being false already makes the whole AND false. The condition order therefore matters for safety. In this lesson use actual Boolean comparisons; Python's and and or can return operand values when given non-Boolean operands.
Explore entry conditions
{"code":"member = False\nweekday = False\nholiday = True\nprint(member and (weekday or holiday))\nprint(member and weekday or holiday)\nprint(member != holiday)\n","instructions":"Predict the three Boolean outputs, then Run. Change member to True, predict/run again, and explain why XOR changes even though ordinary OR would remain true.","files":[],"original_filename":"main.py","runtime_profile":"python-console-v1","file_mode":"fresh"}
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account