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
A
is falsey, the whole expression is false and the result isA
(Python does not evaluateB
); - If
A
is truthy, the result depends onB
and the expression evaluates toB
.
- If
-
A or B
:- If
A
is truthy, the expression is true and the result isA
(Python does not evaluateB
); - If
A
is falsey, the result depends onB
and the expression evaluates toB
.
- If
-
not X
always returns an actual Boolean:True
orFalse
.
Q2. Which statement matches classical logic for "neither X nor Y"?
A) not X or not Y
B) not (X or Y)
C) not X and Y
D) X and not Y
Q3. What does the expression return?
username = ""
result = username or "Anonymous"
A) ""
B) "Anonymous"
C) True
D) False
Answer key: Q1 → False
, False
, True
; Q2 → B; Q3 → B.
12345678# Using the returned operand behavior + short-circuiting print("" or "Guest") # "Guest" (left falsey → returns right) print("User" or "Guest") # "User" (left truthy → returns left) print(0 and 123) # 0 (left falsey → returns left) print(5 and 123) # 123 (left truthy → returns right) print(not 0, not "hi") # True False
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?
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 5
Logical Operators
Pyyhkäise näyttääksesi valikon
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
A
is falsey, the whole expression is false and the result isA
(Python does not evaluateB
); - If
A
is truthy, the result depends onB
and the expression evaluates toB
.
- If
-
A or B
:- If
A
is truthy, the expression is true and the result isA
(Python does not evaluateB
); - If
A
is falsey, the result depends onB
and the expression evaluates toB
.
- If
-
not X
always returns an actual Boolean:True
orFalse
.
Q2. Which statement matches classical logic for "neither X nor Y"?
A) not X or not Y
B) not (X or Y)
C) not X and Y
D) X and not Y
Q3. What does the expression return?
username = ""
result = username or "Anonymous"
A) ""
B) "Anonymous"
C) True
D) False
Answer key: Q1 → False
, False
, True
; Q2 → B; Q3 → B.
12345678# Using the returned operand behavior + short-circuiting print("" or "Guest") # "Guest" (left falsey → returns right) print("User" or "Guest") # "User" (left truthy → returns left) print(0 and 123) # 0 (left falsey → returns left) print(5 and 123) # 123 (left truthy → returns right) print(not 0, not "hi") # True False
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?
Kiitos palautteestasi!