Programming Day 08
Conditional Statements
Introduction
In our daily lives, we often make actions according to how different conditions play
out in a given situation. The conditions determine what actions we will take.
We’ve already been briefly introduced to this when studying our Introduction to
Programming section, but we’ll now focus on doing this in Python.
Flowchart Python
Conditional Statements
Introduction
In programming, we look at a variety of conditions in order to help make
decisions. These are typically called conditional statements. Similarly, in Python
a Boolean expression is when something is either True or False.
Condition #1: Checking for Equality via ==
The simplest conditional test is to see if a variable is equal to a specific value we
are interested in.
The = simply sets the variable car
to the string ‘bmw’
The == checks to see if car is equal
to ‘bmw’
IMPORTANT: Checking for equality
Conditional Statements
Condition #2: Checking for Inequality via !=
In some cases you might need to check that something is NOT equal. In many
programming languages the ! Is linked to “not”.
Condition #3: Comparing Numerical Values
This is pretty easy to do without much difficulty.
You can do many different numerical comparisons as needed.
Conditional Statements
Condition #4: Checking Multiple Conditions using and
There are times where you might need to check that two or more conditions
are True/False.
Condition #5: Checking Multiple Conditions using or
Real life can throw many different conditional needs which might require an or
statement.
Conditional Statements
Condition #6: Checking for a Value in a List
Imagine a situation where you need to check to see if someone ordered a
specific pizza topping for their pizza. A list condition could be needed.
You can also check to see if something is NOT in a list:
If Statements
Introduction
We use the variety of condition types to help us when creating different
condition situations. Understanding conditions is essential to building useful if
statements.
Type #1: Simple If Statements
The simplest if statement has one condition and one action. It’s important to
use a colon and indent properly.
NOTE: You can add multiple lines of code in the indented section as
much
as you need for your program.
If Statements
Type #2: If-Else Statements
Sometimes you need to do one action for one condition but something else for
all other conditions.
Type #3: If-Elif-Else Chains
The above is helpful when there are only two conditions. However, in many
situations there might be multiple conditions that need to be considered.
Your admission
OR cost is $25.
If Statements
Type #4: Multiple elif statements
You can use as many elif blocks that you need for your given program.
Type #5: No else statements
The else code block is simply a catch all. It is helpful to have to ensure that all
conditions get some action. However, in some cases, you can leave it out.
Because all ages are
covered in this if-elif
chain, there is no
need for an else block
If Statements
Type #6: Multiple Condition Testing
The if-elif chain is good when you only need one condition to pass. However, if
you need to check for multiple conditions, you can skip using elif/else
statements and just do a series of if statements.
Using Lists with If Statements
As many programs require the use of different collections like lists, you can
use if statements in some powerful ways to help work with data.
Looking for Specific List Items
Notice below we have a for loop that has an if statement inside of it. Note the
multiple indented sections to ensure the code works properly.
Using Lists with If Statements
Recognizing a List is Empty
There might be a case where a list is initially empty where items might be
added later. It can be helpful to recognize this in your program in some
situations.
The else block is helpful here as it recognizes
that the person hasn’t added any items to
their pizza which may not be desired.
Using Lists with If Statements
Working with Multiple Lists
The power in your programs comes when you can anticipate potential issues
that might arise due to the users working with your program. Notice how this
program helps consider some of these issues.
The else block is helpful here as it looks for
requested toppings that they don’t have.
Using Break/Continue with Loops/Conditions
Moving forward, you will start mixing loops and conditional statements to do all sorts
of things in Python. However, it can sometimes be important to intentionally exit or
continue depending on certain conditions.
Breaking Out of a Loop
You can use break to exit the loop when a certain condition happens.
Continuing from a Loop
You can use continue to skip other code in the loop for the current iteration.
PRACTICE
1. Conditional Tests: Create one conditional tests from one of the options below:
• Tests for equality and inequality with strings
• Try a test using the lower() method to see how it impacts case sensitive situations
• Numerical tests involving equality and inequality, greater than and less than, greater than or equal to, and less than
or equal to
• Tests using the and keyword and the or keyword
• Test whether an item is in a list
• Test whether an item is not in a list
2. Stages of Life: Write an if-elif-else chain that determines a person’s stage of life. Set a value for the variable age, and then:
• If the person is less than 2 years old, print a message that the person is a baby.
• If the person is at least 2 years old but less than 4, print a message that the person is a toddler.
• If the person is at least 4 years old but less than 13, print a message that the person is a kid.
• If the person is at least 13 years old but less than 20, print a message that the person is a teenager.
• If the person is at least 20 years old but less than 65, print a message that the person is an adult.
• If the person is age 65 or older, print a message that the person is an elder.
3. Checking Usernames: Create a program that simulates how websites ensure that everyone has a unique username using
the following steps:
• Make a list of five or more usernames called current_users.
• Make another list of five usernames called new_users. Make sure two of the new usernames are also in the
current_users list.
• Using a for loop, loop through the new_users list to see if each new username has already been used. If it has, print a
message that the person will need to enter a new username. If a username has not been used, print a message saying
that the username is available.
• Make sure your comparison is case insensitive. If 'John' has been used, 'JOHN' should not be accepted.
(To do this, you’ll need to make a copy of current_users containing the lowercase versions of all existing users.)