1 Answers
📚 Quick Study Guide: Python Logical Operators
- 💡 The
andoperator returnsTrueif and only if both operands areTrue. If the first operand isFalse, it immediately returnsFalse(short-circuiting). - 🔗 The
oroperator returnsTrueif at least one of the operands isTrue. If the first operand isTrue, it immediately returnsTrue(short-circuiting). - 🚫 The
notoperator is a unary operator that inverts the boolean value of its operand.not TrueisFalse, andnot FalseisTrue. - ⚖️ Operator Precedence:
nothas the highest precedence, followed byand, thenor. Parentheses()can be used to explicitly control the order of evaluation. - 🔍 Truthiness: In Python, many non-boolean values are considered "truthy" or "falsy". For example,
0,None, empty strings"", empty lists[], and empty dictionaries{}are falsy. Non-empty versions are truthy.
🧠 Practice Quiz: Test Your Logic!
Question 1:
What will be the output of the following Python expression?
print(True and False)
- True
- False
- Error
- None
Question 2:
Consider the following:
x = 10
y = 5
print(x > 5 or y < 3)
What will the output be?
- True
- False
- Error
- 10
Question 3:
Which of the following expressions will evaluate to True?
not (True and True)not False and FalseTrue or not FalseFalse and not True
Question 4:
What is the result of not 0 in Python?
- True
- False
- 0
- Error
Question 5:
Given a = 5 and b = 10, what is the value of (a > 0 and b < 5) or (a == 5 and b == 10)?
- True
- False
- Error
- None
Question 6:
Which operator has the highest precedence among and, or, and not?
andornot- They all have the same precedence
Question 7:
If is_logged_in = True and has_permission = False, what will is_logged_in and not has_permission evaluate to?
- True
- False
- Error
- None
Click to see Answers
1. B (True and False evaluates to False)
2. A (x > 5 is True, so True or y < 3 short-circuits to True)
3. C (True or not False becomes True or True which is True. A: not True is False. B: True and False is False. D: False and True is False)
4. A (In Python, 0 is considered falsy, so not 0 evaluates to True)
5. A ((a > 0 and b < 5) is (True and False) which is False. (a == 5 and b == 10) is (True and True) which is True. So False or True is True)
6. C (not has the highest precedence, followed by and, then or)
7. A (is_logged_in and not has_permission becomes True and not False, which is True and True, evaluating to True)
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀