6.5.2–6.5.3 – Comparisons and Boolean logic

6.5.2–6.5.3 – Comparisons and Boolean logic

Compare values, combine Boolean results and express precise boundary rules. Predict and repair conditions before using them to control branches and loops in later lessons.

Relational comparisons

A condition expresses a yes-or-no decision. A relational operator compares two values and returns Boolean True or False. Later lessons use these results to choose branches and continue loops; here the focus is calculating the condition correctly.

OperatorMeaningExampleResult
==Equal to7 == 7True
!=Not equal to7 != 7False
<Less than7 < 8True
>Greater than7 > 8False
<=Less than or equal to7 <= 7True
>=Greater than or equal to7 >= 7True

A single equals sign assigns; two compare:

Python
target = 12
print(target == 12)
print(target != 12)
Output
True
False

Choose whether a boundary is included. An age of at least 14 uses age >= 14; more than 14 uses age > 14. Exactly 14 distinguishes the two conditions. Numeric comparisons should use numeric values; text follows string-comparison rules and is case-sensitive. The text "Y" does not equal "y".

Combining conditions with and and or

A logical operator combines Boolean results. Python uses lowercase and, or and not. For Boolean conditions, and is true only if both are true; inclusive or is true if either or both are true.

ABA and BA or B
FalseFalseFalseFalse
FalseTrueFalseTrue
TrueFalseFalseTrue
TrueTrueTrueTrue

Use a whole comparison on each side. A requirement that a status is READY or WAIT becomes:

Python
allowed_status = (status == "READY") or (status == "WAIT")

Writing just a nonempty string after or is not a second comparison and does not test the status against that string. Keep the intended two Boolean tests explicit.

Being inside the inclusive range 16 to 24 requires both boundaries. Being outside means either too low or too high:

Python
temperature = 25
inside = (temperature >= 16) and (temperature <= 24)
outside = (temperature < 16) or (temperature > 24)
print(inside)
print(outside)
Output
False
True

At 16 or 24, inside is True and outside is False. Using or between the inside bounds would accept every numeric value: a low value still meets the upper-bound comparison, and a high value still meets the lower-bound comparison.

not and precise grouping

not reverses a Boolean result. It can invert one comparison or a whole bracketed group:

Python
status = "waiting"
print(not (status == "ready"))
level = 10
print(not ((level < 1) or (level > 10)))
Output
True
True

For level 10, both outside-range tests are False. Their or is False, then not changes it to True. For level 11, the upper outside test becomes True, so the final result becomes False. The same inside-range meaning could be written with two inclusive comparisons joined by and.

After comparisons, Python's logical precedence is not, then and, then or. Use parentheses to make the intended grouping easy to inspect. In a permission rule, the difference between A or (B and C) and (A or B) and C matters: only the second requires C for both possible routes.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account