Academy

Conditionals

Programs That Choose

Every program you've written so far has been completely predictable. Given the same input, it always does the same thing, in the same order, without any variation.

Real programs make decisions. A ticket booking system checks whether a seat is available. A login form checks whether the password is correct. A game checks whether the player has enough lives left.

This ability to choose — to run one piece of code and skip another — is what makes programs genuinely useful. In Python, it is done with if.

The if Statement

if condition:
    # code that runs only when condition is True

The condition is any expression that produces True or False. If the condition is True, the indented code runs. If it's False, Python skips it entirely and moves on.

Change 35 to 25 and run it again. Nothing prints. The condition 25 > 30 is False, so Python skips the print() and exits.

Indentation Is Not Optional

Notice that the print() line is shifted inward — indented by four spaces. This is not a style choice in Python. It is the grammar.

The indentation tells Python: these lines belong to the if block. Python uses whitespace to define structure, unlike many other languages that use curly braces {}.

If you forget to indent:

You get an IndentationError. Python refuses to continue. Be consistent — always use four spaces. Most code editors handle this for you automatically.

Adding an Alternative: else

if alone handles one case. else handles everything else.

Exactly one of the two blocks will always run. If the condition is True, the if block runs. If it's False, the else block runs. Never both, never neither.

Try changing 45 to 60 and see the output flip.

Handling Multiple Cases: elif

When there are more than two possibilities, use elif — short for "else if".

Python checks the conditions from top to bottom and stops at the first one that is True. Once a match is found, the rest are skipped entirely.

With score = 72: the first condition 72 >= 90 is False. The second 72 >= 75 is False. The third 72 >= 50 is True — that block runs. Python stops. The else is never reached.

You can have as many elif blocks as you need. The else at the end is optional but acts as a catch-all for anything not covered above.

Conditions Can Be Anything

The condition inside if just needs to be something that resolves to True or False. You already know comparison operators from the previous lesson — all of them work here directly.

Nested Conditions

You can place an if inside another if. This is called nesting.

Read this carefully: the second if only ever runs if the first one passed. If age < 18, Python skips the entire inner block without even looking at it.

Nesting adds power, but too much nesting quickly becomes hard to read. As a habit, prefer flatter structures wherever possible.

Combining Conditions in One Line

You can write compound conditions using and and or directly in the if — no nesting required.

This does the same thing as the nested version above, in fewer lines. When the logic is simple enough to express in one condition, prefer this form.

A Practical Example

Notice that each branch of the if/else uses its own variables — remaining in one, shortfall in the other. Variables created inside a block exist only for that block's purpose. The program always takes exactly one path.

What You Have Learned

Conditionals give programs the ability to make choices — to do different things depending on what's true.

The key ideas:

  • if condition: runs a block only when the condition is True
  • else: provides a fallback for when the condition is False
  • elif: checks additional conditions when the first one failed
  • Python stops checking at the first True condition in an if / elif / else chain
  • Indentation is mandatory — it defines which lines belong to which block
  • and / or let you combine multiple conditions without nesting

In the next lesson, you'll learn about lists — how to store and work with multiple values inside a single variable.