This lesson covers the following topics:
Also known as a conditional structure, a selection structure is a programming feature that performs different processes based on whether a boolean condition is true or false. Selection structures use relational operators to test conditions.
# Here is the syntax of a ONE-WAY selection in Python.
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
password = input("Please enter your password: ")
if password == "12345":
print("Access Granted.")
# Here is the syntax of a TWO-WAY selection in Python.
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
password = input("Please enter your password: ")
if password == "12345":
print("Access Granted.")
else:
print("Access Denied.")
Python supports the usual logical conditions from mathematics. These conditions can be used in several ways, most commonly in "if statements" and loops.
Operator | Meaning | Sample Condition | Evaluates To |
---|---|---|---|
== | equal to | 5 == 5 | True |
!= | not equal to | 5 != 5 | False |
< | less than | 3 < 10 | True |
<= | less than or equal to | 5 <= -2 | False |
> | greater than | 7 > 7 | False |
>= | greater than or equal to | 7 >= 7 | True |
Logical operators allow you to write composite conditional statements. Although there are more logical operators, the three listed below are the most commonly used.
Operator | Meaning | Sample Condition | Evaluates To |
---|---|---|---|
not (!) | (unary operator) negates a boolean value | not True (!True) | False |
and | (binary operator) both sides need to be True to evaluate to True | True and False | False |
or | (binary operator) at least one side needs to be True to evaluate to True | True or False | True |
# Here is an example of "independent" conditional statements. (this program doesn't work as intended!!!)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grade = 91
if grade >= 90:
print("You got an A!")
if grade >= 80:
print("You got a B!")
if grade >= 70:
print("You got a C!")
if grade >= 60:
print("You got a D!")
if grade < 60:
print("You got an F!")
As you can see, the code above doesn't work as intended. The issue is that these are all independent "if" statements. That means each one will be tested. Since 91 is greater than 90, 80, 70, and 60, all of those conditionals evaluate to True and are executed. To fix this, we can link these tests together using if, elif, and else.
# Here is an example of "dependent" conditional statements. (this program works as intended)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
grade = int(input("Please enter your grade: "))
if grade >= 90:
print("You got an A!")
elif grade >= 80:
print("You got a B!")
elif grade >= 70:
print("You got a C!")
elif grade >= 60:
print("You got a D!")
else:
print("You got an F!")
As you can see, the code now works as intended. These conditional tests are now "linked" together with if, elif, and else keywords. This means that once one of them is true, the rest will be skipped, giving us the correct printout.
This example is not meant to show that one method is superior to another. It is meant to show you that you need to program the appropriate structure of your conditional statements to properly solve the problem at hand.
# Here is an example of "nested" conditional statements. (notice how the indentation rules are still applied)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
age = int(input("What is your age? "))
if age >= 16:
if age >= 18:
print("You can vote.")
else:
print("You can drive.")
else:
if age >= 13:
print("You are a teenager.")
else:
print("You are not a teenager yet.")
# Here is an example of that same program as above, but with logical operators.
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
age = int(input("What is your age? "))
if age >= 18:
print("You can vote.")
if age >= 16 and age < 18:
print("You can drive.")
if age >= 13 and age < 16:
print("You are a teenager.")
if age < 13:
print("You are not a teenager yet.")
# Here is an example of that same program as above, but with "dependent" conditional statements.
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
age = int(input("What is your age? "))
if age >= 18:
print("You can vote.")
elif age >= 16:
print("You can drive.")
elif age >= 13:
print("You are a teenager.")
else:
print("You are not a teenager yet.")