# Conditionals & Truthy Logic

> Master Python conditional flow with if, elif, and else, and discover how Python evaluates the inherent truth value of any data structure.

Subject: Python | Lesson 3 of 24 | 5 min read
Interactive lesson: https://codewithanimation.in/python/if-else
Previous: [Operators & Identity](https://codewithanimation.in/python/operators.md) | Next: [For & While Loops](https://codewithanimation.in/python/loops.md)

## Indentation-Based Control Flow

Python controls conditional execution using `if`, `elif` (short for else-if), and `else` statements. Code blocks are delineated strictly by consistent 4-space indentation rather than curly braces or keywords.

An `if` statement evaluates a condition. If the condition is truthy, the indented suite of code executes. If false, Python checks any subsequent `elif` branches in sequence until one matches, or falls back to the optional `else` block.

Python also offers a concise ternary conditional expression for inline assignments: `status = 'adult' if age >= 18 else 'minor'`.

## The Pythonic Truthy and Falsy Model

In Python, every object has an inherent boolean truth value. You do not need to convert objects explicitly with `bool(x)` inside conditional checks.

By standard definition (PEP 8 and the Python Data Model), the following values are considered **falsy**:

Constants: `None` and `False`.

Numeric zeros: `0`, `0.0`, `0j`, and `Decimal(0)`.

Empty sequences and collections: empty string `""`, empty list `[]`, empty tuple `()`, empty dictionary `{}`, and empty set `set()`.

All other objects - including non-empty collections, non-zero numbers, and custom objects that do not define a zero length - evaluate to **truthy**.

## Writing Idiomatic Python Conditionals

Write concise, idiomatic checks by relying on natural truthiness: write `if items:` instead of `if len(items) > 0:`, and write `if not items:` to check for an empty collection.

When evaluating boolean flags, write `if is_ready:` rather than `if is_ready == True:`. Redundant equality checks are un-pythonic and can introduce subtle type conversion bugs.

## Common mistake: The False Zero Trap

Relying on implicit truthiness can cause critical bugs when `0` or empty strings `""` are valid domain inputs. For example, `if count:` evaluates to `False` when `count = 0`, even though a count of zero is a valid numeric value.

When distinguishing between 'missing value' and 'valid zero', always check explicitly against `None`: `if count is not None:`.

## Tip: Chained Comparisons

Python supports mathematical chained comparisons: `if 0 <= score <= 100:` is completely valid and equivalent to `score >= 0 and score <= 100`, but with `score` evaluated only once.

## Try it: The Truthy/Falsy Junction

Feed different Python values into the conditional gate. Watch how the boolean evaluation pipe automatically routes objects to either the if or else branch.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/python/if-else

## Check yourself

### Question 1

Which of the following values evaluates to `False` in a Python if condition?

- A. An empty dictionary {}
- B. The string "0"
- C. A list containing [0]

Correct answer: A

Empty collections, including empty dictionaries `{}`, empty lists `[]`, and empty strings `""`, are falsy by default in Python.

- Why not B: Incorrect. Any non-empty string is truthy, even if it contains the character '0'.
- Why not C: Incorrect. A list with at least one element is non-empty and therefore truthy.

### Question 2

What is the most Pythonic way to test if a list `users` is not empty?

- A. if users:
- B. if len(users) > 0:
- C. if users == True:

Correct answer: A

PEP 8 recommends `if users:` because non-empty collections evaluate to truthy naturally without invoking `len()`.

- Why not B: Incorrect. While functional, it is considered un-pythonic compared to checking truthiness directly.
- Why not C: Incorrect. A list never equals the boolean True.

### Question 3

When should you use `if val is not None:` instead of `if val:`?

- A. When 0, False, or an empty string are valid inputs that should not trigger the else branch.
- B. Only when dealing with custom class instances.
- C. Never, because if val: behaves identically in all situations.

Correct answer: A

If `0` or `""` are valid values in your program, `if val:` will mistakenly skip them because they are falsy. Checking `is not None` ensures only missing values are excluded.

- Why not B: Incorrect. It applies to any data type where falsy values are valid data.
- Why not C: Incorrect. If val is 0, if val: evaluates to False and skips the block.

## Remember this

- Code blocks in Python are defined by 4-space indentation, not braces.
- Empty collections, numeric zeros, None, and False are all inherently falsy.
- Prefer 'if items:' over 'if len(items) > 0:' for readability.
- Use 'if val is not None:' when numeric zero or empty strings are valid data.
- Ternary expressions use the syntax: true_val if condition else false_val.
