Boolean Expressions, Comparisons, and Truthiness
Key Takeaways
- Python boolean literals are exactly True and False; lowercase true or false are names, not boolean constants.
- Comparison expressions produce boolean results, and chained comparisons such as 1 < x < 10 are evaluated as a single combined test.
- The logical operators have a fixed precedence order: not before and, and before or.
- Python treats False, None, numeric zero, and empty collections or strings as falsey; most other values are truthy.
- PCEP boolean questions often hide the answer in truthiness, chained comparisons, short-circuit evaluation, or confusing == with is.
Boolean values and comparisons
Python has two boolean values: True and False. The capitalization matters. true, false, and TRUE are ordinary names, so using them without assignment raises NameError. PCEP questions often test this because beginners remember the idea of true and false but miss Python's exact spelling.
Comparison operators create boolean results:
| Operator | Meaning | Example |
|---|---|---|
== | equal value | score == 70 |
!= | not equal value | name != 'Ana' |
< | less than | x < 5 |
<= | less than or equal | x <= 5 |
> | greater than | x > 5 |
>= | greater than or equal | x >= 5 |
A common exam trap is assignment versus comparison. In Python, x = 3 assigns a value and cannot be used as the condition in if x = 3:. Use x == 3 when asking whether two values are equal.
Chained comparisons
Python supports chained comparisons. This means 1 < x < 10 is not two separate expressions joined later by guesswork. It means 1 < x and x < 10, except x is evaluated only once.
x = 7
print(1 < x < 10) # True
print(1 < x > 10) # False
print(1 == x == 7) # False
When tracing a chained comparison, write each link from left to right. If every link is true, the chain is true. If any link is false, the chain is false.
Logical operators
The logical operators are not, and, and or. Their precedence is:
notandor
So not a or b and c is read as (not a) or (b and c). Parentheses are allowed and should be used in real code, but exam snippets may omit them to test precedence.
Python also short-circuits. For and, if the left side is falsey, Python does not need the right side. For or, if the left side is truthy, Python does not need the right side. At entry level, the main point is that the final truth result may be decided before every subexpression is evaluated.
Truthiness
Conditions do not have to be written as explicit comparisons. Python can test the truth value of many objects:
| Value | Truth value |
|---|---|
False, None | falsey |
0, 0.0 | falsey |
'', [], {}, () | falsey |
| nonzero numbers | truthy |
| nonempty strings and collections | truthy |
Do not confuse the string 'False' with the boolean False. The string is nonempty, so it is truthy.
Trace-table method
For PCEP code-reading questions, do not solve boolean expressions in your head all at once. Use a small table:
| Step | Expression part | Value | Result |
|---|---|---|---|
| 1 | x > 3 | 7 > 3 | True |
| 2 | name | 'Py' | truthy |
| 3 | x > 3 and name | True and truthy | truthy |
Then translate the final truth value into branch behavior. The exam trap is usually one token: an empty string instead of a nonempty one, or instead of and, or a comparison boundary such as < instead of <=.
What is printed by this code? x = 4 print(1 < x < 4 or x == 4)
Which value is truthy in a Python condition?
How is this expression grouped by Python's operator precedence? not a or b and c