Career upgrade: Learn practical AI skills for better jobs and higher pay.
Level up

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.
Last updated: May 2026

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:

OperatorMeaningExample
==equal valuescore == 70
!=not equal valuename != 'Ana'
<less thanx < 5
<=less than or equalx <= 5
>greater thanx > 5
>=greater than or equalx >= 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:

  1. not
  2. and
  3. or

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:

ValueTruth value
False, Nonefalsey
0, 0.0falsey
'', [], {}, ()falsey
nonzero numberstruthy
nonempty strings and collectionstruthy

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:

StepExpression partValueResult
1x > 37 > 3True
2name'Py'truthy
3x > 3 and nameTrue and truthytruthy

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 <=.

Test Your Knowledge

What is printed by this code? x = 4 print(1 < x < 4 or x == 4)

A
B
C
D
Test Your Knowledge

Which value is truthy in a Python condition?

A
B
C
D
Test Your Knowledge

How is this expression grouped by Python's operator precedence? not a or b and c

A
B
C
D