newtum

Our Social Network

LOGICAL OPERATORS IN PYTHON WITH EXAMPLES

Logical Operators in Python with Examples

When you write conditions in Python, you often need to combine more than one test: maybe you want something to happen only if two things are true, or if at least one thing is true, or to invert a condition entirely. That’s exactly where logical operators come in. In this blog post we’ll explore how to use the three main logical operators in Python — and, or, and not — including how they work, where they’re useful, and plenty of concrete examples to help you apply them with confidence.

What Are Logical Operators?

Logical operators in Python are keywords that allow you to build compound Boolean expressions by combining simpler conditions. Rather than just checking a single comparison, you can ask things like “is A true and B true?”, or “is A true or B true?”, or “is it not the case that A is true?” According to multiple sources, Python supports these three operators (and, or, not) to work with Boolean values and more broadly with truth-evaluated expressions.

The and operator

The and operator returns True only if both operands (conditions) are True. If either operand is False, the overall expression is False. For example, when you want to ensure two criteria are met simultaneously.

The or operator

The or operator returns True if at least one of the operands is True. It only yields False when both operands are False. It’s useful when you’re okay with either of two (or more) conditions being satisfied.

The not operator

The not operator is a unary operator (it takes just one operand) and simply reverses the truth value: if the operand is True, not makes it False, and vice-versa. Use it when you want to check that something isn’t the case.

Short-Circuit and Truth Value Details

One important nuance in Python’s logical operators is that they support short-circuit evaluation. That means:

  • For and, if the first operand is False, Python doesn’t bother evaluating the second, because the result will be False anyway.
  • For or, if the first operand is True, Python can skip the second operand because the result will already be True. Also, logical operators in Python don’t only work on strict Boolean values: objects, numbers, sequences have truthy/falsy interpretations (e.g., empty sequence is False, non-empty is True) and you may see and/or returning one of the operands rather than strictly True or False.

Example Code

Here’s a sample Python code block showcasing use of logical operators:

1age = 20
2has_ticket = True
3is_vip = False
4if age >= 18 and has_ticket:
5print("Entry allowed for adult ticket holder.")
6else:
7print("Entry denied.")
8if has_ticket or is_vip:
9print("Access to the event granted.")
10else:
11print("No access: ticket or VIP required.")
12if not is_vip:
13print("Regular guest, no VIP privileges.")

Explanation of the Code

We define three variables: age (an integer), has_ticket (a Boolean), and is_vip (a Boolean).

  • Line 1‐3: We check if age >= 18 and has_ticket:
    • This uses and to require both that age is at least 18 and that has_ticket is True.
    • If both conditions hold, the first print executes; otherwise the else branch runs.
  • Line 4‐8: We then check if has_ticket or is_vip:
    • This uses or, which means if either has_ticket is True or is_vip is True, we print the message granting access.
    • If neither is True, the else runs.
  • Line 9‐11: We execute if not is_vip:
    • Here not reverses the truth of is_vip. Since is_vip is False, not is_vip becomes True, so the message about being a regular guest prints.
  • This small code demonstrates the three operators in action, each in a realistic context.
  • It also hints at typical use-cases: age/ticket access (and), flexible access (or), and exclusion/inversion logic (not).

A Deeper Topic: Operator Precedence & Combining Multiple Logical Tests

When you combine logical operators in one expression, understanding the precedence (order of evaluation) becomes important. In Python, not is evaluated first, then and, then or.For example:

1a = True
2b = False
3c = True
4# Without parentheses
5if a or b and not c:
6print("Condition met.")

In this case, not c is computed first; then b and (not c) is checked; then a or (result) is evaluated. If you intend a different grouping you should use parentheses:

if (a or b) and not c:
...

This will change the logic seam­lessly. Also, because of short-circuiting, you might see slightly surprising behaviour if the second operand has side-effects (calls a function). So it’s best to use parentheses for clarity when combining multiple logical operators. You’ll often see logic like this in real-world code:

if (user_logged_in and has_permission) or is_admin:
allow_action()

Watch Our YouTube Tutorial

Watch our full video breakdown with code execution, tips, and more: Watch on YouTube →

Conclusion

Logical operators in Python—and, or, and not—are foundational tools for governing flow and decision-making in your code. By mastering their behaviour, truth table logic, precedence, and short-circuiting quirks, you’ll write clearer and more correct conditional expressions. Combine this knowledge with the video above and you’re well on your way.

“When you combine conditions with and, you demand all to be true; that’s powering decision logic in code.”

“Using or introduces flexibility: satisfy one of many, and you proceed with your branch.”

“The not operator flips the logic: sometimes you want to check what isn’t, rather than what is.”

Frequently Asked Questions (FAQs)

1. What is the difference between and and & in Python?
and is a logical operator that works on Boolean contexts and supports short-circuiting. & is a bitwise operator (for integers) or logical operator for pandas/NumPy arrays; it does not short-circuit in the same way.
2. Can I chain logical operators like a and b and c?

Yes — that means all a, b, and c must be truthy for the expression to be true. But remember, if a is false, Python stops evaluating further.

3. What happens when I use or with non-Boolean values?

When used with non-Boolean operands, Python returns one of the operands, not necessarily True or False. For example '' or 'hello' yields 'hello'.

4. How do I enforce a specific evaluation order when combining and, or, not?

Use parentheses to group your conditions explicitly, because by default not > and > or. Parentheses override the default precedence.

5. Are logical operators only for Boolean values?

While they’re designed for Boolean logic, in Python truth-value evaluation means many types behave like Booleans (empty vs non-empty sequences, zero vs non-zero numbers) so logical operators work more broadly.

One condition, one comparison, and Python knows what to do next. That’s the power of conditional operators.

— Manoj Kolhe

More Articles

Ready to Explore the Future of Technology?

Unlock the tools and insights you need to thrive on social media with Newtum. Join our community for expert tips, trending strategies, and resources that empower you to stand out and succeed.

Newtum

Newtum is an emerging online academy offering a wide range of high-end technical courses for people of all ages. We offer courses based on the latest demands and future of the IT industry.

© Newtum, Inc. All Rights Reserved.