Skip to content

If statements

If statements

Another piece of programming logic is the if statement. An if statement is a selection structure that will run a section of code if a condition is True and ignore that section of code if the condition is False. In Python, if statements take the following form:

if <true_condition>:
    <code>

The <true_condition> is a statement that must evaluate as either True or False. If the condition is True the <code> indented below the if statement will run. If the condition is False the code indented below that if statement will be skipped. Note the colon : after the if statement and the indentation of the <code> block. An example of an if statement is below:

In [2]:
a = 5
if a<10:
    print('a is less than 10')

a is less than 10

In the first line of code, the variable a was assigned the value 5. The if statement in the second line of code included the Python keyword if and a condition that can be evaluated as True or False. The condition a<10 evaluates to True and therefore the line print('a is less than 10') is run. If we modify the code to assign a=20, then the print() line will not run.
In [3]:
a = 20
if a<10:
    print('a is less than 10')

Else

If statements can include else clauses. An else clause is a section of code that run if the if statement is False. The general form is:

if <true_condition>:
    <code block 1>
else:
    <code block 2>

The else key word needs to be on it's own line and be at the same indentation level as the if keyword that it corresponds to. else needs to be followed by a colon : and any code that is to run as part of the else statement must be indented the same amount. A sample if/else code section is below:

In [4]:
a = 5
if a>10:
    print('a is greater than 10')
else:
    print('a is less than 10')

a is less than 10

Since a=5 assigns a value to a that is less than 10, the code under the if statement does not run. Therefore the code under the else statement does run and a is less than 10 is printed. If the value of a is modified so that it is greater than 10, the code under the if statement will run, and the code under the else keyword will not.
In [5]:
a = 20
if a>10:
    print('a is greater than 10')
else:
    print('a is less than 10')

a is greater than 10

elif

The else if statement can be added to an if statement to run different sections of code depending on which one of many conditions are True. The basic syntax of an else if section of code is:

if <true_condition>:
    <code block 1>
elif <true_condition>:
    <code block 2>
else:
    <code block 3>

The keyword elif must be followed by a logical statement that evaluates to True or False followed by a colon :. The <code block> will run if the else if condition is True and will be skipped if the else if condition is False. An example section of code if below:

In [7]:
color = 'green'
if color == 'red':
    print('The color is red')
elif color == 'green':
    print('The color is green')
else:
    print('The color is not red or green')

The color is green

If we modify the code to assign the string 'orange' to the variable color, the code under the if will not run and the code under the elif will not run either. Only the code under the else will be executed.
In [8]:
color = 'orange'
if color == 'red':
    print('The color is red')
elif color == 'green':
    print('The color is green')
else:
    print('The color is not red or green')

The color is not red or green