Conditions


"If" statements

In order to write proper programs, if statements are necessary. They can be used to execute different code depending on a condition that can be given by a programmer. For example, in the following program, one print() statement is executed if a is equal to 12 and another print() statement is executed if a is equal to 13.

a = 12

if a == 13:
    print("This code is not executed.")

if a == 12:
    print("This code is executed.")

Note that comparison between two variables is done using the double equals operator == because the single equals operator = is used to assign values to variables.

Also note that the code that should be executed in the if statement is indented by 4 spaces compared to the code before. Using this indentation, you can tell the computer which statements belong into the if statement and which are outside of the if statement. In the following example, you can see two print() statements which would be executed only if the variable a would be equal to 13, while the last print() statement is always executed indepented to the variable a:

a = 12

if a == 13:
    print("This code is not executed.")
    print("This line is also not executed.")

print("This is line is executed.")

Boolean operators

The and and or boolean operators allow building complex boolean expressions, for example:

name = "John"
age = 23
if name == "John" and age == 23:
    print("Your name is John, and you are also 23 years old.")

if name == "John" or name == "Rick":
    print("Your name is either John or Rick.")

The "in" operator

The in operator could be used to check if a specified object exists within a list:

name = "John"
if name in ["John", "Rick"]:
    print("Your name is either John or Rick.")

The else and elif statements

While it is possible with an if statement to execute certain code when a condition is True, it is also possible to execute other code if that condition is False using an else statement. Here is an example for this construct:

name = "Franz"

if name == "Hans":
    print("I am Hans.")
else:
    print("I am not Hans.")

Also, it is possible to put multiple if/else statements into each other:

name = "Franz"
has_ice_cream = True

if name == "Hans":
    print("I am Hans.")
else:
    if has_ice_cream:
        print("I am happy.")
    else:
        print("I am not Hans and not happy.")

While this technically works, there is a more elegant and clean way to write code for this using the elif statement. This is a combination of an else and an if:

name = "Franz"
has_ice_cream = True

if name == "Hans":
    print("I am Hans.")
elif has_ice_cream:
    print("I am happy and this code is nice.")
else:
    print("I am not Hans and not happy.")

The "not" and "unequal" operator

Any condition can be inverted using a not before the actual expression. In the following example, the code is only executed when we do not have ice cream:

has_ice_cream = False

if not has_ice_cream:
    print("We are VERY unhappy :'-(.")

This also works with numerical comparisions. For example, in the following, code is only printed if a is not equal to 12.

a = 13

if not a == 12:
    print("a is not 12.")

While this technically works, there is a shorter version for this using the unequal operator !=:

a = 13

if a != 12:
    print("a is not 12.")

Numerical comparisons

For numbers, you can also use comparing operators. On one hand, there are the greater or equals operator >= and the less or equals operators <=. And on the other hand, there are also the greater operator > and the less operator < which become False if the two values are equal.

a = 42

if a > 12:
    print("a is greater than 12.")

if a < 42:
    print("a is less than 42.")

if a <= 42:
    print("a is less or equal than 42.")

They are just operators

Actually, comparison operators can not only be used within if or elif statements. The boolean values True and False are returned when an expression is compared or evaluated. So you can also use them in other contexts like this:

x = 2
print(x == 2) # prints out True
print(x == 3) # prints out False
print(x < 3) # prints out True

This also means that the result can be stored in variables:

x = 2
myvariable = x == 2

print(myvariable)

if myvariable:
    print("x is two.")

Exercise

Change the variables in the first section, so that each if statement resolves as True.

# change this code number = 10 second_variable = 10 first_array = [] second_array = [1,2,3] if number > 15: print("1") if len(second_array) == 2: print("2") if len(first_array) + len(second_array) == 5: print("3") if first_array and first_array[0] == 1: print("4") if not second_variable: print("5") # change this code number = 16 second_variable = False first_array = [1,2,3] second_array = [1,2] if number > 15: print("1") if len(second_array) == 2: print("2") if len(first_array) + len(second_array) == 5: print("3") if first_array and first_array[0] == 1: print("4") if not second_variable: print("5") test_output_contains("1", no_output_msg= "Did you print out 1 if `number` is greater than 15?") test_output_contains("2", no_output_msg= "Did you print out 3 if the length of `second_array` is 2?") test_output_contains("3", no_output_msg= "Did you print out 4 if len(first_array) + len(second_array) == 5?") test_output_contains("4", no_output_msg= "Did you print out 5 if first_array and first_array[0] == 1?") test_output_contains("5", no_output_msg= "Did you print out 6 if not second_variable?") success_msg("Great Work!")
Previous Tutorial Next Tutorial
Copyright © learnpython.org and LeineLab e.V.. Read our Terms of Use.