anthony.hansen
anthony.hansen 6d ago • 10 views

Python 'and', 'or', 'not' operator examples for beginners

Hey everyone! 👋 I've been diving into Python lately, and these 'and', 'or', 'not' operators sometimes trip me up. I get the basic idea, but when they're combined or used in more complex conditions, my brain starts to fizzle. 🤯 Can someone help me get a super clear grasp on how they work, especially with some beginner-friendly examples? A quick study guide and some practice questions would be amazing!
💻 Computer Science & Technology
🪄

🚀 Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

✨ Generate Custom Content

1 Answers

✅ Best Answer

📚 Quick Study Guide: Python Logical Operators

  • 💡 The and operator returns True if and only if both operands are True. If the first operand is False, it immediately returns False (short-circuiting).
  • 🔗 The or operator returns True if at least one of the operands is True. If the first operand is True, it immediately returns True (short-circuiting).
  • 🚫 The not operator is a unary operator that inverts the boolean value of its operand. not True is False, and not False is True.
  • ⚖️ Operator Precedence: not has the highest precedence, followed by and, then or. 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)
  1. True
  2. False
  3. Error
  4. None

Question 2:

Consider the following:

x = 10
y = 5
print(x > 5 or y < 3)

What will the output be?

  1. True
  2. False
  3. Error
  4. 10

Question 3:

Which of the following expressions will evaluate to True?

  1. not (True and True)
  2. not False and False
  3. True or not False
  4. False and not True

Question 4:

What is the result of not 0 in Python?

  1. True
  2. False
  3. 0
  4. 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)?

  1. True
  2. False
  3. Error
  4. None

Question 6:

Which operator has the highest precedence among and, or, and not?

  1. and
  2. or
  3. not
  4. 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?

  1. True
  2. False
  3. Error
  4. 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 In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀