Python If Statements

In programming, If Statements come under the heading of Conditional Logic. They allow you to say what happens if a condition is either true or false.

IF I buy this game

PRINT("I'll be happy")

 

IF my friend is late:

PRINT("I'll leave without her")

 

IF I work hard

PRINT("I'll be successful")

You can also add ELSE parts to your If Statements:

IF I work hard

PRINT("I'll be successful")

ELSE

PRINT("I'll fail")

And even ELSE IF parts:

IF I go to the game

PRINT("I might see my team win")

ELSE IF I go shopping

PRINT("I can buy that game")

ELSE

PRINT("Stay at home and have a bath")

All the statements above can be tested to see if they are true or false. Only one outcome is possible.

Let's get some practice with how Python handles If Statements. We'll use that age variable that we've already got set up from the previous section:

age = input("How old are you? ")

When a user enters an age, we'll use If Statements to print something out.

Delete or comment out any code you have except for the age line. Just below this, enter the following:

if age == 21:

print("You are 21")

Your code should look like this:

Python code for an if statement

Don't run your code just yet. Take a look at the structure of the If Statement:

if age == 21:

print("You are 21")

The word if should be all in lowercase letters. After a space, you type the variable you want to test. Next comes two equals signs, with no space between them. Then you type what it is you're testing your variable for (a value of 21 for us). Finally, you need a colon to end the line. Miss the colon out and you'll get an error message.

NOTE: The two equals sign together mean 'Has a value of'. So we're saying 'If the value inside of the age variable has a value of 21'… Python then works out whether this is true or false. If it's true then the print statement will get executed. If it's not true that the age variable contains a value of 21 then nothing will happen.

The big thing to note, however, is that the second line of your If Statement needs to be indented. You can't put it on the same level as the IF part. So this will get you an error:

if age == 21:

print("You are 21")

How much space do you need for your indent? Well, by convention, you should use 4 spaces. That is, pressing the space bar 4 times. You shouldn't use the tab key as that may get you more (or less) than 4 spaces. PyCharm will automatically indent for you when you press the enter key after an If Statement. The reason for the indent is so that Python knows where your statements begin and end.

Now run your code. Click in the output area, after How old are you?, type 21 as the age and hit enter. What happens? Nothing! You'll just see this:

Output of Python If statement

What went wrong. You entered a value of 21, and that's what ended up in the age variable. So why didn't it print anything.

The reason is that the input method we used is set up to accept text not numbers. When you entered 21, the input method converted it to a string. The If Statement, however, is expecting a number. If it doesn't get a number then the entire statement will be false. Only a value of true will see the print line do its stuff.

So input has turned the age variable into a string. To convert a string to a number in Python you can surround the variable with int():

int(age)

You can also use float(age) if you want to convert to a floating-point value.

Change your If Statement to this:

if int(age) == 21:

print("You are 21")

Run your program again and it should work as expected:

Output window responding to user input

We now need to test for a variety of ages. After all, not everyone is 21!

As was mentioned previously, you can test what else your variable can be. In Python, this is done with the keyword elif (short for else if). You use it like this:

if int(age) == 21:

print("You are 21")

elif int(age) == 22:

print("You are 22")

The elif part goes on the same alignment as the if part. Notice that the two are the same, except for el at the start. Again, if you miss out the colon at the end, you'll get an error message. What you want to happen in your elif part turns out to be true goes on a new and indented line.

Your code should look like this:

Python showing elif

Try it out. Run your code and enter a value of 22. You should see the second print statement in the output window.

You can use an else part to trap any other values:

if int(age) == 21:

print("You are 21")

elif int(age) == 22:

print("You are 22")

else:

print("You are not 21 or 22")

Add the new lines, not forgetting the colon on the end of else. Your code will look like this:

Python code showing example of elif and else

Now try it again. Enter an age other than 21 or 22. The print statement under the else part should appear in the output window.

Aside

Instead of using two equal signs, as we have done above, you can replace them with the keyword is. Like this:

if int(age) is 21:

print("Twenty One")

This is more readable than before. You can use is instead of == from now on in your if statements. However, if you want to transition to other programming languages then the double equal signs are the ones to get used to.

In the next lesson, you'll about operators other than the double equal signs. They are called Conditional Operators. You need to know them.

Conditional Operators >