Logical Operators
People say "yes and yes", "yes or no", "definitely not". In Python, the logical operators and, or, and not express these ideas formally and produce Booleans (True/False) you'll use with comparisons in if/while.
What Each Operator Means
Precedence
not binds tighter than and, which binds tighter than or (not > and > or). Use parentheses when in doubt.
For instance, age >= 18 and has_id is True only if both checks succeed:
123age = 21 has_id = True print(age >= 18 and has_id) # True
Truth Table
A truth table lists all possible truth values of inputs and shows the resulting output of a logical expression. It's a compact way to reason about logic without running code.
Using the Table
If X is False and Y is True, then X and Y is False, X or Y is True, and not X is True.
Return Values & Short-Circuiting (Together)
and and or don't always yield the words True/False, they return one of their operands, and Python may stop early once the result is known.
-
A and B:- If
Ais falsey, the whole expression is false and the result isA(Python does not evaluateB); - If
Ais truthy, the result depends onBand the expression evaluates toB.
- If
-
A or B:- If
Ais truthy, the expression is true and the result isA(Python does not evaluateB); - If
Ais falsey, the result depends onBand the expression evaluates toB.
- If
-
not Xalways returns an actual Boolean:TrueorFalse.
1234567891011121314151617# Choosing a display name for a user profile username = "" # user didn't set a custom name print(username or "Guest") # "Guest" β fallback to default name username = "Alice" print(username or "Guest") # "Alice" β custom name is used # Checking login attempts and access permissions login_attempts = 0 access_level = 123 print(login_attempts and access_level) # 0 β login not yet attempted login_attempts = 5 print(login_attempts and access_level) # 123 β user has access after attempts # Checking empty and non-empty values print(not 0, not "Hello") # True False β 0 is falsey, non-empty string is truthy
1. Fill the blanks with True or False). Let X = True, Y = False.
2. Which statement matches classical logic for "neither X nor Y"?
3. What does the expression return?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 5.26
Logical Operators
Swipe to show menu
People say "yes and yes", "yes or no", "definitely not". In Python, the logical operators and, or, and not express these ideas formally and produce Booleans (True/False) you'll use with comparisons in if/while.
What Each Operator Means
Precedence
not binds tighter than and, which binds tighter than or (not > and > or). Use parentheses when in doubt.
For instance, age >= 18 and has_id is True only if both checks succeed:
123age = 21 has_id = True print(age >= 18 and has_id) # True
Truth Table
A truth table lists all possible truth values of inputs and shows the resulting output of a logical expression. It's a compact way to reason about logic without running code.
Using the Table
If X is False and Y is True, then X and Y is False, X or Y is True, and not X is True.
Return Values & Short-Circuiting (Together)
and and or don't always yield the words True/False, they return one of their operands, and Python may stop early once the result is known.
-
A and B:- If
Ais falsey, the whole expression is false and the result isA(Python does not evaluateB); - If
Ais truthy, the result depends onBand the expression evaluates toB.
- If
-
A or B:- If
Ais truthy, the expression is true and the result isA(Python does not evaluateB); - If
Ais falsey, the result depends onBand the expression evaluates toB.
- If
-
not Xalways returns an actual Boolean:TrueorFalse.
1234567891011121314151617# Choosing a display name for a user profile username = "" # user didn't set a custom name print(username or "Guest") # "Guest" β fallback to default name username = "Alice" print(username or "Guest") # "Alice" β custom name is used # Checking login attempts and access permissions login_attempts = 0 access_level = 123 print(login_attempts and access_level) # 0 β login not yet attempted login_attempts = 5 print(login_attempts and access_level) # 123 β user has access after attempts # Checking empty and non-empty values print(not 0, not "Hello") # True False β 0 is falsey, non-empty string is truthy
1. Fill the blanks with True or False). Let X = True, Y = False.
2. Which statement matches classical logic for "neither X nor Y"?
3. What does the expression return?
Thanks for your feedback!