SypherTec
…
PART 2 · 02 OF 083 MIN READUPDATED 20 NOV 2026

if and else: Choosing What Runs

Your tracker says Over budget: False. A person reading the report would rather see a plain message, and a warning only when one is needed. This lesson makes the program choose which lines to run.

The concept

Here is your tracker as the last lesson left it. Run it:

WHERE YOU LEFT OFF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Now run this. The total is over the budget:

THE CONDITION IS TRUE · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Three lines printed. Now the same code with a bigger budget:

THE CONDITION IS FALSE · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Only Report finished printed this time. The two indented lines were skipped.

The line if total > budget: is an if statement. Python works out the comparison. When it is True, Python runs the indented lines under it. When it is False, Python skips them and carries on from the first line that is not indented.

Those indented lines are called a block. Indentation is how Python knows where the block starts and stops. This is your first block, and every block in Python works the same way.

Often you want one thing or the other. Run this:

TWO PATHS, ONE RUNS · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

else: gives the if a second block. Exactly one of the two blocks runs: the if block when the condition is True, the else block when it is False.

The syntax

if total > budget:
    print("Status: over budget")
    print("Cut back before the month ends.")
else:
    print("Status: within budget")
  • if, then a condition, then a colon.
  • The block underneath is indented by four spaces. Every line in it must have the same indentation.
  • else: sits at the same indentation as its if, and takes no condition. It ends with a colon too.
  • The block ends at the first line that goes back to the indentation of the if.

In this sandbox, press Enter at the end of a line that ends in a colon, and the editor indents the next line for you. Press Shift+Tab to step back out of a block. Most code editors behave the same way.

A minimal working example

main.py
total = 12500
budget = 15000
 
if total > budget:
    print("Status: over budget")
else:
    print("Status: within budget")
print("Report finished")

Output: Status: within budget, then Report finished. The last line is not indented, so it runs whichever way the choice went.

Common mistakes

You forget the colon. Run this:

NO COLON · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
SyntaxError: expected ':'

Every line that opens a block ends with a colon: if, else, and the others you will meet in this part. Python names the missing character.

You forget to indent the block. Run this:

NO INDENTATION · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
IndentationError: expected an indented block after 'if' statement on line 3

The if line promised a block, and the next line was not indented. Add four spaces in front of it.

You use one equals sign in the condition. Run this:

ONE EQUALS SIGN IN THE CONDITION · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

A condition must be a question. = stores a value, so it is not one. Python's first suggestion is the one you want: ==. You can ignore := for now.

Your indentation is uneven. If one line in a block has four spaces and the next has two, Python stops with IndentationError: unindent does not match any outer indentation level. Give every line in a block the same indentation.

You give else a condition. else total < budget: gives SyntaxError: expected ':'. else never takes a condition: it covers every case the if did not. The next lesson shows how to check a second condition.

Try it yourself

You will replace the bare True or False with a message your tracker chooses.

  1. Run the starting code below. Confirm the last line is Over budget: followed by True or False.

  2. Delete the over_budget = total > budget line and the Over budget print at the end. Under the Budget print, add this block, indented as shown:

    if total > budget:
        print("Status: over budget")
        print("Cut back before the month ends.")
    else:
        print("Status: within budget")

    Run it. Confirm the last line is Status: within budget if your total is below your budget.

  3. Test the other path: change your budget to 10000 and Run it. Confirm the report ends with the two over-budget lines instead. Then put your budget back.

  4. Break it on purpose: delete the four spaces in front of print("Status: within budget"). Run it. Confirm the console shows an IndentationError that says it expected an indented block after the 'else' statement.

  5. Fix it: put the four spaces back. Run it. Confirm your status line returns.

TRY IT YOURSELF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Your tracker now decides what to say. It has only two answers, though: over or within. A total ₦100 under budget gets the same calm message as a total ₦10,000 under. The next lesson adds a third answer in between.

QUIZ · 3 QUESTIONSCheck what stuckStart the quiz
PART 2 · 02 OF 08❮❯