← Back to Blog

What If One Thing Is True and the Other Isn't?

The problem...

You know how to check one condition.

Is the age above 18? Is the price below 100?

But real programs rarely check just one thing at a time.

What if you need both to be true? What if only one needs to be true? What if you need the opposite?

The idea!

Python has three logical operators that let you combine or modify conditions.

  • and — both must be true
  • or — at least one must be true
  • not — flips true to false and false to true

Making it real — and

You want to enter a club. You need to be over 18 AND have an ID.

age = 20
has_id = True

print(age > 18 and has_id)   # True — both conditions met

If either condition is false, the result is false.

age = 16
has_id = True

print(age > 18 and has_id)   # False — age condition fails

Making it real — or

You get a discount if you're a student OR a senior.

is_student = False
is_senior = True

print(is_student or is_senior)   # True — at least one is true

Only if both are false, the result is false.

is_student = False
is_senior = False

print(is_student or is_senior)   # False — neither is true

Making it real — not

You want to check if something is NOT true.

is_banned = False
print(not is_banned)   # True — flipped
is_banned = True
print(not is_banned)   # False — flipped

In practice

age = 20
has_id = True
is_banned = False

print(age > 18 and has_id and not is_banned)   # True

All three conditions in one line. Python reads left to right.

Going further

Logical operators can be combined with comparison operators freely.

price = 85
is_member = True

print(price < 100 or is_member)    # True
print(price < 50 and is_member)    # False
print(not is_member or price < 50) # False

What's really happening

Logical operators take bool values and return a bool value.

You're not doing math. You're building logic — and logic is the foundation of every decision a program makes.

Heads up!

  • and returns True only if BOTH sides are true
  • or returns True if AT LEAST ONE side is true
  • not flips the value — not True is False, not False is True
  • You can chain multiple conditions — Python reads left to right

The mindset shift

Stop thinking: "I can only check one thing at a time."

Start thinking: "I can combine conditions to express any logic I need."

What you should understand now

  • and — both must be true
  • or — at least one must be true
  • not — flips the bool value
  • Logical operators always return a bool
[ login to bookmark ] // copied! 35 views · 2 min
// resources
Code Example logical_operators.py
← prev Everything is True or False next → Is It in There?
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.