Introduction to Wagtail | Coders of Colour

If statements

This is one of the most important concepts of programming (Bill Gates talks about it all the time).

Often in programming, the instructions we give to the computer dependent on certain actions or values.

For example:

If someone is under 18, they can't vote. Else they can vote.

if age < 18 :
print("You can't vote")
else:
print("You can vote")

The code above is called an if statement.

We can also add supplementary conditions to if statements - elif. An elif is just like an if, it just has a different condition.

Let's take the example from before:

if age < 18 :
print("You can't vote.")
elif age == 18:
print("Congratulations! You can vote")
else:
print("You've been voting.")

if and elif must have conditions after them. The computer checks if the value of the condition is True in order to decide what to do. If the condition is True, the code below the if or elif is executed.

If the condition is False the code below the else is executed.

To show equality in Python, we use two =, because one = is used for variable declaration.

Edit this page on GitHub

On this page