Python If Statement: If Else and Elif Explained

About 35 minutes

What you need before this lesson
  • Variable
  • String
  • Integer
  • Float
  • Boolean
  • List
  • Dictionary
  • print
  • type
  • key
  • set
What this lesson adds
  • if
  • else
  • elif
  • Comparison Operators
  • and
  • or
  • not

The counter at the top counts this lesson's exercises (8 of them). Reading and running the examples don't count.

So far we know how to store data, like:

But Python is still just holding the data. We want it to start making decisions based on that data, to tell it, for example: if the page is a 404, print that we have a problem. That's what conditions are for.

What's a condition?

A condition is simply a question whose answer is True or False. If we ask status_code == 404 and status_code really is 404, the answer is True. But if status_code = 200 and we ask the same question, the answer is False.

The rule
A condition is a test that gives back True or False.

The difference between = and ==

This is one of the most important things for a beginner. When you write status_code = 404, the = sign means: store the value 404 in the variable status_code. But when you write status_code == 404, the == sign means: is the value of status_code equal to 404?

The rule
= stores a value, and == compares two values.

We haven't used if yet. We're just making sure we understand what a condition is.

Without running it, predict the output:

Predict the output

What will the two lines print?

Now, if

The word if means just that: if. When we write if status_code == 404: we're telling Python: if status_code equals 404, run the line below.

Example
Expected output
Broken page
Audit finished

Notice that print("Audit finished") runs either way, because it isn't inside the if. But what if status_code were 200 instead of 404?

Predict the output

What will the code print if status_code = 200?

The colon and indentation

Notice that the if line ends with a colon :. It means: the code that belongs to this condition starts here. And that brings us to something very important in Python: indentation. The space before print() isn't just formatting. Python relies on it to know that the line belongs to the if. We usually use 4 spaces.

This code is wrong: the second line isn't indented under the if
Check your understanding

What happens if you run the code above?

Look at it this way too: if print("Broken page") is inside the if with a space and print("Audit finished") is completely outside it, then indentation is what decides which code belongs to the condition and which code is outside it. That stays with us for the whole course.

Comparisons aren't just ==

The main comparison operators are: == equal to, != not equal to, > greater than, < less than, >= greater than or equal to, and <= less than or equal to. Let's see them in context:

Example
Expected output
Long page
Thin content
Example
Expected output
Page is not 200
High traffic page
Top 10

Now we add else

We know if means: if the condition is true, do this. But sometimes we want to say: and if it isn't true, do something else. That's what else is for:

Example
Expected output
Page is working

Notice that if and else are at the same level, but the code under each one is indented 4 spaces.

What if there are more than two possibilities?

For example, status_code could be 200, 301, 404 or 500, not just right or wrong. Here we use elif, short for else if: if the first condition wasn't met, try the next one.

Example
Expected output
Redirect

Python works from top to bottom. Is 301 == 200? No. Is 301 == 301? Yes, so it prints Redirect and stops there in the if/elif/else chain.

An important difference between if and elif

Look at this example:

Example
Expected output
Thin content
Missing title

Each if here stands on its own. But what if we write the second one as elif instead of if?

Predict the output

What will the code print if we use elif instead of the second if?

Because the first condition was met, Python doesn't go through the rest of the elif chain. This matters a lot in SEO: a page can have more than one problem at the same time, so you'll usually use several separate ifs, not elif.

The boolean shortcut

In lesson 1 we saw that True and False are a data type called bool. If indexable = True, we can write if indexable == True: and that's correct, but we can also write it more simply:

Example
Expected output
Page is indexable
Page is indexable

Both give the same result, because indexable already is True. At the start, make sure you understand the full form if indexable == True: before you use the shortcut if indexable:.

Logical operators

Sometimes one condition isn't enough. Say you want: the page returns 200 and it isn't indexable. That's two conditions. This is where we need and, or and not.

and

and means: the first condition and the second condition both have to be true. If either one is False, the code doesn't run.

Example
Expected output
Allowed
Note
The rule for and: True and True → True. True and False → False. False and True → False. False and False → False. So and is strict: it wants both conditions true.

An SEO example of and:

Example
Expected output
Live page but not indexable

What if the page had status_code = 404 instead of 200?

Predict the output

What will the code print if status_code = 404?

The condition isn't met, because the first part, status_code == 200, is now False.

or

or is different: one true condition is enough.

Example
Expected output
Weekend
Note
The rule for or: True or True → True. True or False → True. False or True → True. False or False → False. So or needs just one condition to be true.

An SEO example of or: if the page is a 404 or a 500, it has an error.

Example
Expected output
Page has an error

not

not is different from both. It doesn't join two conditions; it flips the value: True becomes False, and False becomes True.

Example
Expected output
True

And we can use it inside a condition. The full form is very clear, but Python lets us write it more simply:

Example
Expected output
Not indexable
Not indexable

At the start, I'd rather you understand if indexable == False: first, and then start using if not indexable: once you know what it's short for.

Putting it all together on an SEO example

Here's a page:

Let's check it with four conditions that don't depend on each other:

Example
Expected output
Missing title
Thin content
Not indexable

Notice we used several ifs, on purpose, because the page can have more than one problem at once. Now let's try and in the same example: we want to catch a page that returns 200 but can't be indexed.

Example
Expected output
Live page but not indexable

Both conditions are true here: status_code == 200 is true, and indexable == False is true, so True and True gives True and the code runs. Now let's try or: if the page is a 404 or a 500, treat it as an error.

Predict the output

What will this code print?

Our page is a 200, so False or False gives False, and nothing is printed.

One last important point

When Python sees word_count < 300, it doesn't see the words " Thin Content". It works out 180 < 300, which is True, then if True:, so it runs the code. In the end, an if works on True/False, and that's the most important thing I want you to take from this lesson.

Check your understanding

When Python sees if word_count < 300:, what actually happens?

Lesson exercise

Here's a page:

SEO challenge

Check the page with the right conditions

Write conditions that print "Page is OK" if status_code equals 200, "Page redirects" if it equals 301, and "Page not found" if it equals 404, using a single if/elif chain. Separately, print "Thin content" if word_count is under 300, and print "Not indexable" if the page isn't indexable.

In the next lesson we start loops, explained the same way: before we write for page in pages, we'll understand what a loop is, why we need it, and what for and in mean.

You still have exercises left

0/8

Exercises left: 8 of 8. The lesson counts as complete once you solve them all, and you can come back to them any time.

Well done, you reached the end of the lesson.