Boolean Expressions: Teaching Code to Make Decisions

We introduced the Boolean data type in a previous post. It seems very simple — the value can be either True or False.

Is it really that simple? Let’s see for ourselves.

These two variables each hold a Boolean value:

dog_has_four_legs = True
truck_is_small = False

⚠️ Note that the capitalization is important! True and False must start with uppercase letters.

true, FALSE, TRUE → ❌ not valid in Python

This is the simplest way to assign a Boolean value. But in real programming, we almost never type True or False directly. Instead, those Boolean results are almost always derived from expression evaluation.

Let’s dive a bit deeper into how this evaluation works.


Equality and Inequality

To check for equality, we use the == operator (double equals).
To check for inequality, we use the != operator.

2 + 2 == 5  # False
2 + 2 != 5  # True

Double equals means equal. Exclamation-equals means… nope!


⚖️ Comparison Operators

Here are the comparison operators you can use:

  • > (greater than)
  • < (less than)
  • >= (greater than or equal)
  • <= (less than or equal)

Example:

2 + 2 > 5  # False
2 + 2 < 5  # True

Math battles: who is bigger?


🚫 Boolean Negation (not)

not True   # False
not False  # True

not: flipping True to False and False to True — like a light switch!

This might seem trivial, but it’s useful when checking that a condition does not match something:

not (1 == 1)  # False
not (1 == 0)  # True

If you want the opposite answer, just say not.


🔗 Multiple Conditions: and / or

To check that all conditions are true, use and:

2 + 2 == 4 and 3 + 3 == 6 # True
2 + 2 == 5 and 3 + 3 == 6 # False
2 + 2 == 4 and 3 + 3 == 5 # False
2 + 2 == 5 and 3 + 3 == 5 # False

AND: everyone on the team must say ‘True’.

To check if at least one condition is true, use or:

2 + 2 == 4 or 3 + 3 == 6 # True
2 + 2 == 5 or 3 + 3 == 6 # True
2 + 2 == 4 or 3 + 3 == 5 # True
2 + 2 == 5 or 3 + 3 == 5 # False

OR: at least one True and we’re happy!

Parentheses can help readability:

(2 + 2 == 4) and (3 + 3 == 6) # True

Spacing does not affect the result.


🔠 Comparing Strings

'john' == 'john'  # True
'john' == 'bob'   # False

Strings must match exactly — same letters, same case.

String comparison is case-sensitive:

'john' == 'John'  # False

Case matters! ‘john’ is not ‘John’.

Python can also compare strings alphabetically (lexicographical order):

'john' > 'bob'  # True

Comparing words like they’re in the dictionary.

It’s based on Unicode ordering — we won’t go too deep here.

⚠️ Be careful with number-like strings:

'2' > '10'  # True  → ('2' comes after '1' alphabetically)

When numbers pretend to be strings, logic gets silly.


🚫 Comparing Different Types

The comparison of different data types usually results in False, even if the values look similar:

'2' == 2  # False

A string is a string. A number is a number.

Even lists vs tuples:

simple_list = [1, 2]
simple_tuple = (1, 2)
simple_list == simple_tuple  # False

Lists ≠ Tuples (even if they seem best friends).

However, Python can implicitly convert some values:

2 == 2.0  # True (int casted to float)

Python sneakily converts types when it can.

And:

1 == True   # True
0 == False  # True

True acts like 1, False acts like 0 — surprise!


📦 Membership: Checking Inside a Collection

Using in keyword:

For list:

scandinavian_countries = ['Denmark', 'Norway', 'Sweden']
'Denmark' in scandinavian_countries  # True
'Brazil' in scandinavian_countries # False

Checking if something is part of a group.

And tuple:

scandinavian_countries_tuple = ('Denmark', 'Norway', 'Sweden')
'Denmark' in scandinavian_countries_tuple # True
'Brazil' in scandinavian_countries_tuple  # False

Search inside lists and tuples like a membership check.

Even strings:

'a' in 'apple'  # True

Finding characters hiding inside a word.


📝 Quick Recap Suggestions

You can include one at the end like:

Tiny Recap

  • ==, != → check equality/inequality
  • <, >, <=, >= → compare numbers
  • not, and, or → combine conditions
  • Strings comparisons are case-sensitive
  • 'value' in collection → item exists?

Common Mistakes to Watch Out For

  • Using = (assignment) instead of == (comparison)
  • Forgetting that "2" and 2 are not the same type
  • Misjudging string comparison ('10' < '2' → True 😬)
  • Boolean with numbers: True == 1, False == 0

There are many more Boolean expression evaluations to explore! 😄
These are the basics that will help us when we start learning flow control next!


🧪 Boolean Basics — Mini Challenges

Try answering these without running Python first — just think through them!
(Then test in Python to check if you’re right 😌)


Challenge 1 — What’s the Result?

Predict the output (True or False):

  1. 3 > 5
  2. 7 == 7.0
  3. "Hello" == "hello"
  4. 10 != 2 * 5
  5. True and False
  6. False or 5 > 2

Challenge 2 — Fix the Comparison

Which of these comparisons are incorrect? Fix them so Python won’t error:

A. "5" > 3
B. 10 = 10
C. 3 <> 1 (old-school surprise 👀)


Challenge 3 — Truthy or Falsey?

Are these considered True or False when converted to a boolean?

  1. 0
  2. 1
  3. "" (empty string)
  4. " " (a space)
  5. [] (empty list)
  6. "hello"

👇 Spoilers Below! Scroll only after attempting!

.
.
.
.
.
.
.

🧁 Boolean Challenge Solutions

✔ Challenge 1

  1. 3 > 5False
  2. 7 == 7.0True (same numeric value)
  3. "Hello" == "hello"False (case matters!)
  4. 10 != 2 * 5False (2*5 = 10)
  5. True and FalseFalse
  6. False or 5 > 2True (5 > 2 is True, and or only needs one True)

✔ Challenge 2

A. "5" > 3 ❌ (string vs number not allowed)
Fix: compare same types —
int("5") > 3 → True
or
"5" > "3" → True (string comparison by character order)

B. 10 = 10 ❌ (= is assignment)
Fix: 10 == 10 → True

C. 3 <> 1 ❌ (Python 2 relic)
Fix: 3 != 1 → True


✔ Challenge 3 — Truthiness

ExpressionBoolean ValueWhy
0FalseZero means “nothing here”
1TrueAny non-zero number is True
""FalseEmpty string = False
" "TrueA space is still something
[]FalseEmpty container = False
"hello"TrueNon-empty content
  1. 3 > 5False
  2. 7 == 7.0True (same numeric value)
  3. "Hello" == "hello"False (case matters!)
  4. 10 != 2 * 5False (2*5 = 10)
  5. True and FalseFalse
  6. False or 5 > 2True (5 > 2 is True, and or only needs one True)


You’ve unlocked the power of Boolean logic — the key to decision-making.
Next stop on our programming journey: Flow Control, where your code learns to react, choose paths, and avoid danger like a real adventurer! ⚔️✨


Leave a Reply

Your email address will not be published. Required fields are marked *