LEARN Python - 2.5 boolean logic Python’s Way of Saying Yes or No - zerotopyhero.com

Learn Python – 2.5: Boolean Logic – Python’s Way of Saying Yes or No

“Boolean logic” sounds like one of those terms that belongs in a textbook with very small print.

In reality, it’s much simpler than it sounds.

Boolean logic is just how Python combines yes-or-no answers.

That’s it.

No philosophy.
No mind games.
Just clear rules for combining truths.

If you can answer questions like:

  • “Is this true and that true?”

  • “Is this true or that true?”

  • “Is this not true?”

Then you already understand boolean logic.

You just haven’t called it that yet.

First: What Does “Boolean” Even Mean?

A boolean is a value that can only be one of two things:

  • True

  • False

That’s it.
No maybes.
No kinda.
No “it depends.”

Every comparison you’ve written so far produces a boolean.

				
					5 > 3        # True
10 == 7      # False
age >= 18    # True or False
				
			

Boolean logic is what lets Python combine those results.

The Three Boolean Operators You Need to Know

Python has three main boolean operators:

  • and

  • or

  • not

They work exactly like they do in plain English.

and – Both Things Must Be True

and means:

“This and that must both be true.”

Example:

				
					age = 20
has_id = True

if age >= 18 and has_id:
    print("You may enter.")

				
			

Python reads this as:

  • Is age at least 18?

  • Does the person have ID?

  • Are both answers yes?

If either one is false, the whole thing is false.

Quick mental shortcut:

  • True and True → True

  • Anything else → False

or – At Least One Thing Must Be True

or means:

“This or that is enough.”

Example:

				
					is_weekend = True
is_holiday = False

if is_weekend or is_holiday:
    print("You can sleep in!")

				
			

Python checks:

  • Is it the weekend?

  • Is it a holiday?

  • If either one is true, run the code.

Shortcut:

  • False or False → False

  • Anything else → True

not – Flip the Answer

not simply reverses a boolean.

  • not True → False

  • not False → True

Example:

				
					is_raining = False

if not is_raining:
    print("No umbrella needed.")

				
			

This reads as:

“If it is not raining…”

Which sounds exactly like normal language.

Combining Comparisons with Boolean Logic

This is where things get powerful.

				
					age = 17
has_permission = True

if age >= 18 or has_permission:
    print("Access granted.")
else:
    print("Access denied.")
				
			

Even though the age is too low, permission saves the day.

Python doesn’t care why something is allowed.
It only cares whether the logic says “yes” or “no.”

Reading Boolean Logic Like a Human

When beginners struggle, it’s usually because they try to read code like math.

Don’t.

Read it like a sentence.

This:

				
					if age >= 18 and country == "DK":

				
			

Becomes:

“If the age is at least 18 and the country is Denmark…”

If it sounds sensible in English, it usually works in Python.

A Common Beginner Trap (And How to Avoid It)

This is wrong:

				
					if age == 18 or 19:   # No good
				
			

Why?

Because Python reads it as:

  • Is age equal to 18?

  • Or is 19 true?

And 19 is always true.

The correct version is:

				
					if age == 18 or age == 19:   # Good
				
			

Or better:

				
					if age >= 18:
				
			

Clarity beats cleverness every time.

You Can Store Boolean Results Too

Booleans can live in variables:

				
					is_adult = age >= 18

				
			

Later:

				
					if is_adult:
     print("Adult confirmed.")

				
			

This makes code easier to read and reason about.

What You’ve Learned

You now understand:

  • What booleans are

  • How and, or, and not work

  • How to combine multiple conditions

  • How Python evaluates logic

  • How to read logic like normal language

This is real thinking code now.

Mini Quiz

Try these:

  1. What values can a boolean have?

  2. When does and return True?

  3. When does or return False?

  4. What does not True evaluate to?

  5. What will this print?

				
					if is_adult:
     print("Adult confirmed.")

				
			
ZeroToPyHero