Unit 4 - If Condition
4.0 - Table of Contents
- 4.1 - Example
- 4.2 - Conditions
- 4.3 -
ifStatements - 4.4 - Process List with
if - 4.5 - Formatting Guide
- 4.6 - Sample Code

4.1 - Example
- some code on runs on certain conditions
- e.g. check if person is over 18
Example: if_intro.py
4.1.1 - Combine with Traversal
- this can be combined with list traversal to increase efficiancy of code
Example:
for_if.py
4.2 - Conditions
- the core of an
ifstatement is in its conditional statement - Code in
ifwill only run when the condition isTrue - Boolean value
- if the condition is
False, the code inelsewill run elseis optional
4.2.1 - Check if Equal
- to check if two values are the same, use
==and palce the two values to check on each side. - if the two values are identical then it returns
TrueelseFalse =is define while==is comapre Example:double_equal.py
4.2.2 - To Check Letter Cases or Not
- when comapring two strings, they have to be identical in order to return
True - even letter cases
- to ignore letter cases, unify them with
.lower(),.upper(), or.title()Example:letter_cases.py
4.2.3 - Check if Unequal
- use
!=to check if they are unequal !meansnotExample:not_equal.py
4.2.4 - Compare Numbers
- numbers follows the same rules
Trueif the two numbers are the same andFalseif not>,>=,<,<=Example:compare_numbers.py
4.2.5 - Multiple Conditions
- sometimes you may want to check multiple conditions
andandoris useful in connecting these conditions
and- To check if both conditions are
True - only
Trueif both areTrue - if one is
Falsethen it isFalse - Truth Table
|
A|B|A and B| | :—–: | :—–: | :—–: | |True|True|True| |True|False|False| |False|True|False| |False|False|False| Example:and.py
- To check if both conditions are
or- To check if any conditions are
True - returns
Trueif either one isTrue - if both is
Falsethen it isFalse - Truth Table
|
A|B|A and B| | :—–: | :—–: | :—–: | |True|True|True| |True|False|True| |False|True|True| |False|False|False| Example:or.py
- To check if any conditions are
4.2.6 - Check if Value is in List
- before certain actions the values of the list need to be checked
- e.g. before register a user, check if the username already exists
- use keyword
into check if value is in list Example:in.py
4.2.7 - Check if Value is not in List
- it is also important to check if values are not in a list
- e.g. ban list for a website
- key
not inis used Example:not_in.py
4.3 - if Statements
- there are many types of
ifstatements ifif-elseif-elif-else- …
4.3.1 - Simplest if strcture
- one condition and one action
- first line includes the condition
- next indented line includes the action
- the indented line only runs when the
ifstatement isTrueExample:if.py
4.3.2 - if-else strcture
- two routes depending on the condition
Trueruns the code under theifFalseruns the code under theelse- “Do A if condition is true, else do B”
Example:
if-else.py
4.3.3 - if-elif-else strcture
- multiple possible actions depending on the cases
Example:
if-elif-else.py
4.4 - Process List with if
- combining
forloop andifstatement can process special elements in a list - making sure the code will work in any conditions
4.4.1 - Special Element in List
- sometimes certain values requires special treatment
Example:
toppings.py
4.4.2 - Check if List is empty
- there was an assumption that there is at least one element in list
- sometimes something special need to be done when list is empty
Example:
empty.py
4.5 - Formatting Guide
- to make the code easier to read, a space (` ) is usually added to either side of the condition
- does not affect how the code runs
- asthetic choices
a == b-
a==b
4.6 - Sample Code
if_intro.py
age = 30
if age >= 18:
print("adult")
else:
print("child")
Output:
adult
for_if.py
ages = [17, 18, 32, 68, 2, 10]
for age in ages:
if age >= 18:
print("adult")
else:
print("child")
Output:
child
adult
adult
adult
child
child
double_equal.py
name = "Ava":
print(age == "Ava")
print(age == "Ethan")
Output:
True
False
letter_cases.py
name = "Ethan Hu"
print(name == 'ethan hu')
print(name.lower() == 'ethan hu')
Output:
False
True
not_equal.py
name = "Ethan Hu"
print(name != 'John')
print("John" != 'John')
Output:
True
False
compare_numbers.py
age = 18
if age < 18:
print("not yet adult")
if age == 18:
print("just turned adult")
if age >= 18:
print("adult")
Output:
just turned adult
adult
and.py
age1 = 18
age2 = 17
print(age1 >= 18 and age2 >= 18)
Output:
False
or.py
age1 = 18
age2 = 17
print(age1 >= 18 or age2 >= 18)
Output:
True
in.py
name = "Ethan"
invitations = ["Ethan", "Ava", "John", "Bob"]
print(name in invitations)
Output:
True
not_in.py
name = "Dillon"
banned = ["Ethan", "Ava", "John", "Bob"]
print(name not in invitations)
Output:
True
if.py
if 10 > 9:
print("10 is larger than 9")
Output:
10 is larger than 9
if-else.py
if 9 > 9:
print("9 is larger than 9")
else:
print("9 is not larger than 9")
Output:
9 is not larger than 9
if-elif-else.py
age = 12
if age < 4:
print("baby")
elif age < 12:
print("kid")
elif age < 18:
print("teenager")
else:
print("adult")
Output:
teenager
toppings.py
requested_toppings = ["mushrooms","green pepper", "olive","extra cheese"]
stock = ["olive", "extra cheese", "pepperoni"]
for topping in requested_toppings:
if topping in stock:
print("Adding" + topping)
else:
print("We are out of " + topping)
print("Pizza is finished")
Output:
We are out of mushrooms
We are out of green pepper
Adding olive
adding extra cheese
Pizza is finished
empty.py
requested_toppings = []
stock = ["olive", "extra cheese", "pepperoni"]
if requested_toppings:
for topping in requested_toppings:
if topping in stock:
print("Adding" + topping)
else:
print("We are out of " + topping)
print("Pizza is finished")
else:
print("Just a plain pizza?")
Output:
Just a plain pizza?