Skip to content
Python for Data Science
Python Fundamentals 8 min read

Conditionals & Booleans

Teach your code to make decisions — comparison operators, and/or/not, if/elif/else chains, truthiness, and the pitfalls that bite beginners.

Download notebook Open Google ColabIn Colab: File → Upload notebook → pick the downloaded file.

Real programs make decisions: flag an outlier, grade a score, route a customer. Everything hinges on questions that evaluate to True or False. In this lesson you'll master comparisons and boolean logic, then use if/elif/else to branch your code — plus the handful of pitfalls that account for most beginner bugs.

Comparison operators produce booleans

You met these briefly in the data-types lesson. Every comparison evaluates to a bool, and you can store that result in a well-named variable:

Python — runs in your browser

= assigns, == compares

The single biggest beginner mistake: score = 85 STORES 85 into score, while score == 85 ASKS whether score equals 85. Python will refuse to run an assignment where a condition belongs (a SyntaxError inside an if), but typing == where you meant = fails silently - the comparison result is just thrown away. When a variable mysteriously never changes, check for this.

Combining conditions: and, or, not

Real rules usually involve several conditions at once. Python combines booleans with plain English words:

  • a and bTrue only if both are true
  • a or bTrue if at least one is true
  • not a — flips the value
Python — runs in your browser

That last line is a lovely Python idiom — 60 <= score < 85 reads exactly like the math notation and replaces an explicit and. Use it whenever you're checking that a value falls inside a range.

if / else: your first branch

An if statement runs its indented block only when the condition is True; the optional else block runs otherwise. Indentation isn't decoration in Python — it is the block structure:

Python — runs in your browser

elif: many branches, first match wins

When there are more than two outcomes, chain conditions with elif ("else if"). Python checks each condition top to bottom and runs only the first block whose condition is true — the rest are skipped:

Python — runs in your browser

Notice the second branch is just score >= 60, not score >= 60 and score < 85. It doesn't need the upper bound: if the score were 85 or more, the first branch would already have caught it. Ordering your conditions from strictest to loosest keeps each one simple — and getting that order wrong is a classic logic bug (put score >= 50 first and everything above 50 lands there).

Nested conditions

An if can live inside another if — just indent one level deeper. Use nesting when a second decision only makes sense after the first:

Python — runs in your browser

Nesting more than two levels deep gets hard to read. Often you can flatten it with andif age >= 17 and is_member: — or by returning early. Prefer whichever version you can read aloud without stumbling.

Truthiness: non-booleans in conditions

Python lets any value stand in a condition. Empty things — 0, "", [], {}, None — count as False; everything else counts as True. This is called truthiness, and it makes "is there anything here?" checks very concise:

Python — runs in your browser

One caution: truthiness can't distinguish "missing" from "legitimately zero". If 0 is a valid value in your data, test explicitly with value is None instead of if value:.

Ternary expressions: if in one line

When each branch just picks a value, Python's conditional expression squeezes the whole decision into one line: value_if_true if condition else value_if_false.

Python — runs in your browser

Keep ternaries for genuinely simple picks. The moment you're tempted to nest one inside another, switch back to a full if/elif block.

Check your understanding

5 questions · free
  1. Q1.What is the difference between score = 85 and score == 85?

  2. Q2.score is 70. What does the chain if score >= 50: ... elif score >= 60: ... elif score >= 85: ... print?

  3. Q3.Which expression is equivalent to 60 <= score < 85?

  4. Q4.Which of these values is truthy?

  5. Q5.What does "pass" if score >= 60 else "fail" do when score is 59?

Exercise: A shipping-cost calculator

Write shipping_cost(weight, is_member=False) for a delivery service. Rules: a weight of 0 or less is invalid — return None; up to 1 kg costs 10000; up to 5 kg costs 25000; above 5 kg costs 25000 plus 4000 for every kg over 5. Members always get 10% off the final price (use a ternary for the discount). Test it with weights 0.5, 3, 8, 8 with membership, and -2.

Next up: loops — running the same logic over every item in your data, automatically.