Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

A condition on its own only produces True or False. To make the computer actually do something different depending on that answer, the condition has to be attached to an if. That is selection: the code that makes the computer perform different tasks in different situations. It is sometimes called a decision, which is a good way to think about it.

Selection with one outcome

The simplest form of selection has a single outcome. Something either happens or it does not.

weather = "rain"

if weather == "rain":
    print("Take your umbrella")
Take your umbrella

There are three parts to that piece of code, and all three are needed:

  1. The keyword if, followed by the condition.
  2. A colon at the end of the if line. It marks the end of the condition and announces that a block of code is coming.
  3. One or more indented lines underneath. Those lines are the branch: the code that runs when the condition is true.

Now change one thing, the value stored in weather:

weather = "sun"

if weather == "rain":
    print("Take your umbrella")

This time the program appears to do nothing at all. It produces no output and no error. That is not a bug: the condition came back False, so Python skipped the indented line underneath it, and there was nothing else left in the program to run.

A program with an if and nothing else can finish without printing anything. When that happens, the condition was false and the branch was skipped. It is easy to think the code is broken when it is doing exactly what you asked.

The indentation decides what is inside the branch

Indentation is not decoration in Python. It is how the language knows where the branch stops. Compare these two programs, which differ only in how far the last line is indented.

Here the second print is indented, so it is part of the branch and is skipped along with the first one:

weather = "sun"

if weather == "rain":
    print("Take your umbrella")
    print("Off to school")

The program outputs nothing.

Here the second print starts at the left margin, so it is not part of the branch. It sits after the if and runs whatever the weather is:

weather = "sun"

if weather == "rain":
    print("Take your umbrella")
print("Off to school")
Off to school

Indent every line of a branch by the same amount. Four spaces is the usual choice in Python, and the editor adds them for you when you press enter after a line that ends in a colon.

Selection with two outcomes

One if gives you one outcome. Very often you want two: one thing when the condition is true, and a different thing when it is false. That is what else is for.

mark = 55

if mark >= 40:
    print("You passed")
else:
    print("You failed")
You passed

else is a catch all. When the condition after the if is true, Python runs the indented lines under the if and skips straight past the else. When the condition is false, Python skips the if branch and runs the indented lines under the else instead. Exactly one of the two branches runs, every single time.

The else line needs a colon and an indented branch of its own, just like the if does.

else never carries a condition. Writing else mark < 40: is an error.

It does not need one, because there is nothing left to check. else means “in every other case”, and every other case is precisely what is left over when the if condition is false.

Forgetting the colon

The colon is the part people leave off most often. Python cannot guess where the condition ends without it, so it stops before running anything:

SyntaxError: expected ':'

If a program refuses to start and the error mentions a colon, look at the end of your if and else lines.

Try it yourself

Run the code below and type rain when it asks. Then run it again and type sun, and then Rain with a capital R.

print("What is the weather like?")
weather = input()

if weather == "rain":
    print("Take your umbrella")
else:
    print("No special advice for you")

Once all three answers make sense, delete the else line and the line under it, and run the program again with rain and with sun. With the else gone you are back to one outcome, so one of those two answers now produces no output beyond the question itself.

Help! My code doesn't work!

Make sure that you check for the following things:

  • There is a colon (:) at the end of every if line and every else line.
  • Every line inside a branch is indented, and all of them by the same amount. Never mix tabs and spaces in one file.
  • There is no condition after else.
  • You have used a double equals (==) in a condition that checks whether two pieces of data are the same. A single = stores data in a variable.
  • You have remembered the brackets after input.