6. Conditionals

In this lesson you will learn about selection and how to run certain lines of code depending on a condition.

What's Selection?

Selection in computer science is when the computer selects certain code to run and not others. Think of it as do blank if blank. To do this in python we use boolean expressions and the if statement. Below is a very simple example of determining if someone is minor:

age = int(input("What is your age? "))
if age <= 17:
    print("You are a minor")

Note how after the if statement there is a colon and all the "selected" code to run if the if statement is true is indented.

You can either indent with a tab (4 spaces) or two spaces. It is all up to personal preference.

The boolean expression is after the if; In this case it was age <= 17. If the boolean expression is true, the indent code is ran, if not the indented code is skipped over. There are multiple different operators to compare in boolean expressions below are some:

num = 3
if num > 4: # greater than sign, False
    pass
if num >= 10: # greater than or equal to sign, False
    pass
if num < 4: # less than sign, True
    pass
if num <= 10: # less than or equal to sign, True
    pass
if num == 3: # equal to sign, True
    pass
if num != 3: # not equal to sign, False
    pass

Notice how the equal to sign is == not =.

The pass statement is generally used as a placeholder for future code. When the computer reads a pass statement, it just simply does nothing and skips the line.

But what if we wanted to see if someone was a minor or an adult? To do this we need an if else statement. Basically if the if statement is false, the code under the else statement runs. Here is an example:

age = int(input("What is your age? "))
if age <= 17:
    print("You are a minor")
else:
    print("You are an adult")

But what if we wanted to do multiple if statements and only choose one? To do this we need an else if statement (aka elif). The computer starts at the first if statement, and checks if it is true or false. If it is false then it moves on to the next else if statement and checks again to see if it is true or false. If false it will then go onto the next statement. Below is an example:

age = int(input("What is your age? "))
if age <= 17:
    print("You are under 18")
elif age <= 40:
    print("You are under 41")
else:
    print("You are over 40")

Note how we put the <= 17 statement before the <= 40 statement or else the <= 17 statement would never happen since 17 <= 40.

But what if we want to check multiple conditions at once? To do this we can utilize "and" and "or" operators. "and" is true only when both conditions are met whereas "or" is true only when either one condition is met or both conditions are met. Below are some examples of and and or being used:

x = 5
if x > 3:
    if x < 10:
        print("x is greater than 3 and less than 10")
# is the same as:
if x > 3 and x < 10:
    print("x is greater than 3 and less than 10")

y = 5
if x > 3 or y == 10: # in this case x > 3 is true and y == 10 is false
    print("x is greater than 3 or y is 10")

Notice how there are two if statements nested; The if statement inside of another if statement is indented.

But what if we wanted the opposite, if something was "not" true? This is where the "not" keyword comes into play. Below is an example:

x = 3
if not x == 1:
    print("x is not equal to 1")

y = False
if not y:
    print("y is not true")
❮ PreviousNext ❯