Booleans, comparisons, and if/else

Make decisions with True/False, comparisons, and if / elif / else — and understand truthiness.

🌱 Module 0 6 min read Not started

Why this matters

All the arithmetic in the world is useless if a program cannot make a decision. True/false questions are everywhere: "Is the user signed in?", "Is the price over 100?", "Did the file end?". Booleans are Python's way of representing true/false, and if statements are how you make your program take different paths based on them. This is the moment programs stop being recipes and start being logic.

Learning objectives

After this lesson you will be able to:

  1. Create and understand True / False.
  2. Compare values with ==, !=, <, >, <=, >=.
  3. Combine conditions with and, or, not.
  4. Write an if / elif / else block that takes different paths.
  5. Understand "truthiness" — how values behave as true/false even when they are not booleans.

1. Booleans — True and False

A boolean is a value that is either True or False. Note the capital letters — Python is case-sensitive.

python
is_running = True
is_done = False

print(is_running)    # True
print(is_done)       # False

You usually do not write True / False directly. You compute them with a comparison.


2. Comparisons — asking questions that answer true/false

These operators return a boolean:

OperatorMeaningExampleResult
==equal5 == 5True
!=not equal5 != 3True
<less than3 < 5True
>greater than5 > 3True
<=less or equal5 <= 5True
>=greater or equal5 >= 9False
python
age = 30

age == 30      # True
age != 30      # False
age > 18       # True
age <= 21      # False

The classic beginner trap: = vs ==

  • = assigns: x = 5 puts 5 into x.
  • == compares: x == 5 asks "is x equal to 5?" and returns a boolean.
python
if score = 100:    # ERROR — this assigns, and is not allowed in an if
if score == 100:   # OK — this asks "is score equal to 100?"

3. Combining conditions: and, or, not

You can combine boolean values into bigger questions.

  • and — both must be true.
  • or — at least one must be true.
  • not — flips true to false and vice versa.
python
age = 30
has_id = True

age > 18 and has_id     # True and True -> True
age > 18 or has_id      # True
not has_id              # False
python
score = 85

if score >= 80 and score <= 100:
    print("Great score!")

You can chain comparisons directly in Python:

python
if 80 <= score <= 100:
    print("Great score!")

This reads naturally and is the Pythonic way.


4. if / elif / else — making a choice

The if statement runs a block of code only when its condition is True.

python
age = 20

if age >= 18:
    print("You are an adult.")

else — the other path

python
age = 15

if age >= 18:
    print("You are an adult.")
else:
    print("You are not yet an adult.")

elif — more than two paths

"elif" is short for "else if". Run the first condition that is true; skip the rest.

python
score = 85

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "F"

print(grade)    # B

Python checks each if/elif top to bottom and takes the first one that is true. Order matters — notice we check >= 90 before >= 80, or an 88 would wrongly match >= 80 only after we've already ruled out the higher grade.

Indentation is the grammar

In Python, the indentation (4 spaces) at the start of a line defines which block a statement belongs to. It is not optional. Whatever is indented under the if runs when the condition is true.

python
if age >= 18:
    print("adult")     # indented: runs only if the condition is true
    print("welcome")   # also runs only if true
print("done")          # NOT indented: always runs

5. Truthiness — values that behave like True/False

This is a Python quirk that surprises newcomers but is very handy. When you put a non-boolean value in an if, Python treats some values as "falsey" and everything else as "truthy".

The falsey values are:

  • False
  • None
  • 0 and 0.0
  • "" (empty string)
  • [], {}, set() (empty collection)

Everything else is truthy.

python
name = "Ada"

if name:
    print("The name is not empty")
else:
    print("The name is empty")

Here name is non-empty, so it is truthy and we print the first line. If name were "", it would be falsey.

This lets you write short, readable checks. But be aware of it — a common bug is checking if x: when you meant to test whether x equals a specific value.


Common mistakes

  1. = instead of == in an if — the classic. Remember == asks a question.
  2. Forgetting the colon at the end of the if / else line.
  3. Inconsistent indentation — mixing tabs and spaces, or varying the number of spaces. Use 4 spaces everywhere. Your editor should do this.
  4. Wrong elif order — check the most specific case first.
  5. and vs or mix-up — and requires both; or needs just one. Be explicit when you write it out loud.

Hands-on lab (45 minutes)

  1. Write an if that prints "big" if a number is over 100, else "small".
  2. Given age and has_license, print "can drive" only if age >= 18 AND has_license is true.
  3. Write a grade function snippet that maps a score to A/B/C/F using elif. Test it with 95, 85, 75, and 50.
  4. Print "even" if a number is even (n % 2 == 0) else "odd".
  5. Use an f-string inside an else branch: print a message that includes one of your variables.
  6. Challenge: a username is valid if it has at least 3 characters and contains no spaces. Write the check and print "valid" / "invalid".

Self-check

  1. What is the difference between = and ==?
  2. What does True and False evaluate to? True or False?
  3. Which two values are "falsey" besides False?
  4. Why does order matter in an if / elif chain?
  5. What does if name: check when name is a string?

References

Sign in to save your progress and earn badges.