01 / 21
T F F T

Boolean
Expressions

Master True, False, and the logical operators that power every decision your Python programs make.

Python

What Is a Boolean?

A Boolean is the simplest data type in Python. It can hold exactly one of two values: True or False. Every time your program makes a decision, it evaluates a Boolean expression behind the scenes.

# Two possible Boolean values is_raining = True has_umbrella = False print(type(is_raining)) # <class 'bool'>

Expressions That Return True or False

A Boolean expression is any expression that Python evaluates to True or False. Comparison operators are the most common way to build them.

==
Equal to
!=
Not equal to
< >
Less / Greater than
<= >=
Less/Greater or equal
print(5 > 3) # True print(10 == 7) # False print(4 <= 4) # True

Why Booleans Matter

Without Boolean expressions, programs would run the same way every time. Booleans give programs the ability to branch, loop, and react to different inputs.

Start
\( \text{age} \geq 18 \)?
Boolean check True Grant access False Deny access Continue

The Three Logical Operators

Python provides three keywords to combine or invert Boolean expressions. These are the building blocks of complex logic.

and
True only if both sides are True
or
True if at least one side is True
not
Flips True to False and False to True
x = 5 print(x > 0 and x < 10) # True print(x > 10 or x == 5) # True print(not (x > 10)) # True

Truth Tables for and, or, not

Truth tables show every possible outcome. Memorize these patterns: they never change.

ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
ABA or B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

and vs or: The Key Difference

and or
Returns True when Both operands are True At least one is True
Returns False when Any operand is False Both are False
Real-world analogy "You need a ticket and an ID" "Cash or credit card"
Short-circuits at First False First True

Parentheses Control Logic

Just like in math, parentheses override the default evaluation order. Python evaluates not first, then and, then or. Parentheses let you force any grouping you want.

# Without parentheses: and binds tighter than or True or False and False # True (and first) # With parentheses: force or first (True or False) and False # False

Operator Precedence

Python evaluates operators in a strict order from highest to lowest precedence. Parentheses always win.

1
( ) : Parentheses are evaluated first, innermost to outermost
2
Comparisons : ==, !=, <, >, <=, >= are evaluated next
3
not : Inverts the Boolean value it precedes
4
and : Combines two expressions (both must be True)
5
or : Combines two expressions (at least one must be True)

How to Evaluate Step by Step

Follow this checklist every time you read or write a compound Boolean expression:

1
Find the parentheses. Evaluate the innermost group first.
2
Evaluate comparisons. Replace each comparison with True or False.
3
Apply not. Flip any value preceded by not.
4
Apply and. Both sides must be True to remain True.
5
Apply or. Only one side needs to be True.

Example 1: Simple Expression

age = 16 has_permit = True can_drive = age >= 16 and has_permit print(can_drive) # ?
1
Evaluate age >= 16 → 16 >= 16 → True
2
Evaluate True and True → True

Example 2: Parentheses Change the Result

x = 3 # Version A: no parentheses x > 5 or x < 10 and x != 3 # Version B: with parentheses (x > 5 or x < 10) and x != 3
Version A
and first: True and False → False
Then or: False or False → False
Version B
( ) first: False or True → True
Then and: True and False → False

Example 3: not with Compound Logic

logged_in = False is_admin = True show_login = not logged_in and not is_admin print(show_login) # ?
1
not logged_in → not False → True
2
not is_admin → not True → False
3
True and False → False

Example 4: Nested Parentheses

temp = 72 weekend = True raining = False go_outside = (temp > 60 and temp < 90) and (weekend or not raining)
1
First ( ): \( 72 > 60 \) → True, \( 72 < 90 \) → True, True and True → True
2
Second ( ): not raining → True, weekend or True → True
3
Combine: True and True → True

Common Mistakes

✕ Using = instead of ==
x = 5 assigns a value. x == 5 checks equality. Mixing them up is a syntax error in an if-statement.
✕ Forgetting precedence
Writing a or b and c when you mean (a or b) and c. The and binds first without parentheses.
✕ Double negatives
Writing not (x != 5) when x == 5 is simpler and clearer. Avoid unnecessary not with !=.

Summary Reference

Operator Syntax Returns True when Precedence
Comparisons == != < > <= >= Condition is met Highest
not not x x is False High
and x and y Both x, y are True Medium
or x or y At least one is True Low
( ) (expression) Forces grouping Overrides all

Question 1 of 5

What does the following expression evaluate to?
\( \text{not } (\text{True and False}) \)

Question 2 of 5

Given \( x = 7 \), what does this return?
\( x > 5 \text{ or } x < 3 \text{ and } x == 7 \)

Question 3 of 5

Which operator has the highest precedence among the logical operators?

Question 4 of 5

What is the output of this code?

a = True b = False print((a or b) and (not b))

Question 5 of 5

Which line of code correctly checks if score is between 80 and 100 inclusive?