---
title: Conditionals in Python (if, elif, else)
source: https://app.sythra.ai/learn/python/conditionals-in-python
topic: Python
updated: 2026-08-12
publisher: Sythra (https://app.sythra.ai)
---

# Conditionals in Python (if, elif, else)

Conditionals in Python let a program run different code depending on a condition, using if to run code when true, elif to check additional possibilities in order, and else as a catch-all default.

_Source: [https://app.sythra.ai/learn/python/conditionals-in-python](https://app.sythra.ai/learn/python/conditionals-in-python) — free to read on Sythra._

## Key points

- if runs its body only when the condition evaluates to True
- pass is a placeholder statement for a branch you haven't written yet
- else runs when the if condition is False; exactly one of the two branches always runs
- elif chains check conditions top to bottom — only the first match runs
- Deeply nested if statements can usually be flattened with and/or for readability

**Conditionals in Python** — `if`, `elif`, and `else` — are how you teach a program to choose. Once you can build boolean expressions with [comparison operators](/learn/python/comparison-operators-python), conditionals are what actually put those questions to use, letting a program run different code depending on what it finds.

## What you will learn

- How `if` runs code only when a condition is true
- The `pass` statement for planning branches you haven't written yet
- How `else` handles the opposite case
- Chaining multiple possibilities with `elif`
- Nested conditionals — and when to flatten them with `and`/`or` instead

## Conditional execution: teaching your program to choose

The `if` statement is how you tell Python: _run this only if the condition is true._

```python
if x > 0:
    print('x is positive')
```

- `if` — the keyword that starts a decision
- `x > 0` — the **condition**, a boolean expression that Python evaluates
- `:` — the colon tells Python "here comes the body of this decision"
- `print('x is positive')` — the **body**, indented four spaces, running only if the condition was `True`

If the condition is `False`, Python completely skips the indented body and moves on to whatever comes next — no error, no message, just silence. This structure looks just like function definitions: a header ending in a colon, followed by an indented body. That is not a coincidence — Python uses this same pattern for almost everything that has "a header and a block that belongs to it."

### pass: a placeholder for code you haven't written yet

Sometimes you want to plan out a branch but have not written the code for it yet. Python provides `pass` for exactly this situation — a statement that does nothing at all, but satisfies Python's requirement that the body cannot be empty:

```python
if x < 0:
    pass    # TODO: handle negative numbers later
```

Think of `pass` as a sticky note on an empty shelf — the shelf is there, the label is there, but you have not put anything on it yet.

## Alternative execution: what about the other case?

An `if` on its own only handles the case where the condition is true. Sometimes you want something to happen in _both_ cases — that is what `else` is for:

```python
if x % 2 == 0:
    print('x is even')
else:
    print('x is odd')
```

Now there are two paths: if `x % 2 == 0` is `True`, the first block runs; if it is `False`, the `else` block runs instead. One of these two will _always_ run — there is no situation where neither happens. These two paths are called **branches**, like a fork in a road — you go one way or the other, never both.

## Chained conditionals: handling more than two possibilities

Two branches covers a lot of situations, but not all of them. What if you have three, four, or more possible outcomes? There is a cleaner way than separate `if` statements: `elif`, short for "else if."

```python
if x < y:
    print('x is less than y')
elif x > y:
    print('x is greater than y')
else:
    print('x and y are equal')
```

Python works through these from top to bottom, checking each condition in order. The moment it finds one that is `True`, it runs that branch and completely skips the rest — even if a later condition would also be true. The `else` at the end is optional, acting as a catch-all that runs if nothing above it matched.

Here is a real-world-style example — responding to a user's menu choice:

```python
if choice == 'a':
    draw_a()
elif choice == 'b':
    draw_b()
elif choice == 'c':
    draw_c()
```

You can have as many `elif` branches as you need — there is no limit. Only the _first_ matching one will run, so put the most specific or most important checks at the top.

## Nested conditionals: conditions inside conditions

You can place an `if` statement inside another `if` statement. This is called **nesting**:

```python
if x == y:
    print('x and y are equal')
else:
    if x < y:
        print('x is less than y')
    else:
        print('x is greater than y')
```

This works, but notice how each layer of nesting pushes the code further to the right. Add a few more levels and it becomes very hard to read — every level asks the reader to track another "are we inside this condition?" in their head.

Whenever you find yourself nesting, ask whether logical operators could flatten things out instead:

```python
# Nested — two layers of indentation, harder to follow
if 0 < x:
    if x < 10:
        print('x is a positive single-digit number.')

# Flattened with and — one layer, reads almost like English
if 0 < x and x < 10:
    print('x is a positive single-digit number.')
```

Both do the exact same thing. The second one is just much easier to read. As a general rule: if you can avoid nesting by using `and` or `or`, do it.

> **Worth knowing:** 

## Common mistakes

- Forgetting the colon at the end of an `if`, `elif`, or `else` line
- Inconsistent indentation, which causes an `IndentationError`
- Writing an `elif` that can never be reached because an earlier condition already covers it
- Nesting three or four levels deep when `and`/`or` would flatten the logic
- Forgetting a default `else` branch and silently doing nothing for unexpected input

> **Practice with Sythra:**  [Practice with AI tutor](https://app.sythra.ai/pricing)

## FAQ

### What is the difference between if, elif, and else in Python?

if checks a condition and runs its body if true. elif (else if) checks another condition only if the previous ones were false. else is a catch-all that runs if none of the if/elif conditions matched.

### What does the pass statement do in Python?

pass does nothing — it's a placeholder used when Python requires a body (like inside an if block) but you haven't written the logic yet.

### Can you have multiple elif statements in Python?

Yes, there's no limit on the number of elif branches. Python checks them top to bottom and runs only the first one that matches, skipping the rest.

### What is a nested conditional in Python?

A nested conditional is an if statement placed inside another if statement's body. It works, but deep nesting hurts readability — combining conditions with and or or often flattens the same logic into one level.

### What happens if no if/elif condition matches and there's no else?

If none of the conditions are true and there is no else block, Python simply skips the entire if/elif chain and continues with the next statement — no error, no output.

## Related

- [Comparison Operators in Python](https://app.sythra.ai/learn/python/comparison-operators-python) — Building the boolean expressions conditionals rely on.
- [The match-case Statement in Python (Python's Switch)](https://app.sythra.ai/learn/python/match-case-python) — match/case, default cases, and dictionary dispatch.
- [Recursion in Python](https://app.sythra.ai/learn/python/recursion-in-python) — Base cases, stack frames, factorial, and Fibonacci.
- [Python course hub](https://app.sythra.ai/learn/python) — All free Python explainers and the path into Agentic practice.
- [Try/Except and Exception Handling in Python](https://app.sythra.ai/learn/python/try-except-python) — Catching errors gracefully with try, except, else, finally.

---

Written by Sythra — Learn machine learning by building. Practice this topic with Sythra's AI tutor: https://app.sythra.ai/pricing
