Chapter 3.2.1 - if
statement review¶
The most basic if
statements syntax is as follows:
if (test):
code
In this example, code
will only run if test
is True.
Much like the evaluation order for setting variables, test
is the first thing that is processed in the if
statement.
The only requirement for test
is that it is a single bool
after it is evaluated. This is simple if only one test is used:
temperature = 50
if (temperature > 95):
code
In this case, temperature > 95
is recognized by Python as a test that needs to be changed into a bool
. Since 50 is not greater tha 95, the bool
in this case is False
.
Chapter 3.2.2 - Complex conditions¶
What if you needed multiple tests? For example, say that you need to make sure the temperature was between 0 C and 100 C:
temperature = 20
condition1 = temperature >= 0
condition2 = temperature <= 100
How can we use both condition1 and condition2? By using the following keywords:
and
or&
: this combines two tests by requiring both tests to beTrue
to result inTrue
or
or|
: this combines two tests by requiring at least one test to beTrue
to result inTrue
Here is a basic example with two conditions a
and b
combined with and
:
a = True
b = False
print("A =", a)
print("B =", b)
if a and b:
print("both a and b are True")
else:
print("a, b, or both a and b are False")
Here is the same example, except combined with or
a = True
b = False
print("A =", a)
print("B =", b)
if a or b:
print("Either a is True, b is True, or both a and b are True")
else:
print("both a and b are False")
We can combine as many tests as we like, as long as you can simplify the tests down to a single bool
Try to change a
, b
, c
, and d
to get a different result.
a = True
b = False
c = True
d = False
print("A =", a)
print("B =", b)
print("C =", c)
print("D =", d)
if (a and b) or (c and d):
print("either a and b are both True OR c and d are both True")
else:
print("either a and b are False AND either c or d are False")
Slowing down: Different ways to think about this:¶
1. Use bool
math:
A very low-level way to look at this is to convert True
to 1 and False
to 0 in your head.
If you have two conditions that are either True
(1) or False
(0), and
will require the sum to be 2 to result in True
, whereas or
will require the sum to be at least 1 to result in True
.
This can be helpful for complex conditions:
a = True
b = False
c = True
d = False
e = True
d = False
Do order of operation from left to right and based on the parentheses:
Complex condition to solve -> (a or b) and (c or (d and e)) or (f)
(a or b)
->(True or False)
-> (1 + 0) -> 1 (True
since 1 is >= 1 for OR) -> 1(d and e)
->(False and True)
-> (0 + 1) -> 1 (False
1 < 2 for AND) -> 0(c or (d and e))
->(True or (False))
(1 + (0)) -> 1 (True
since 1 is >= 1 for OR) -> 1(a or b) and (c or (d and e))
->(True) and (True)
-> (1) + (1) -> 2 (True
since 2 >= 2 for AND)(a or b) and (c or (d and e)) or (f)
->(True) or (False)
-> (1) + (0) -> 1 (True
since 1 >= 1 for OR)
This will result in True
. How would you change the code below to result in False
?
a = True
b = False
c = True
d = False
e = True
f = False
(a or b) and (c or (d and e)) or (f)
2. Use “truth tables”
You can look at every possible combination of two bool
by using truth tables. These tables simulate what would happen if one of the four possible combinations of and
or or
occurred:
and
truth table:
Condition 1 | Condition2 | Condition 1 and Condition 2 |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
or
truth table:
Condition 1 | Condition2 | Condition 1 or Condition 2 |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Example: if Condition 1 is False
and Condition 2 is True
, we can interpret the truth tables as follows:
and
truth table:
Condition 1 | Condition2 | Condition 1 and Condition 2 | Result |
---|---|---|---|
True | True | True | |
False | False | False | |
True | True | False | ✔ |
False | False | False |
or
truth table:
Condition 1 | Condition2 | Condition 1 or Condition 2 | Result |
---|---|---|---|
True | True | True | |
True | False | True | |
False | True | True | ✔ |
False | False | False |
Combining or
and and
to see all possible combinations for both cases, and specifically if a
is False
and b
is True
:
Variable 1 | Variable 2 | Conditional Test 1 | Conditional Test 2 | Result |
---|---|---|---|---|
a | b | a and b | a or b | |
True | True | True | True | |
True | False | False | True | |
False | True | False | True | ✔ |
False | False | False | False |
Try modifying the numbers below to see how it changes the results.¶
What does the following code do “in English”?
temperature = 0
humidity = 100
if (temperature <= 32) and (humidity == 100):
print("freezing fog")
elif (temperature >= 32) and (humidity == 100):
print("fog")
else:
print("clear")
Chapter 3.2.3 - Practice¶
You have an observation that includes (unit or format, data type):
- temperature (degrees F, float)
- year (YYYY, int)
- month (MM, int)
- state (abbreviation, str).
You want to print temperature only if the following conditions are met:
- The
year
is before 2000 - The
month
is either June, July, or August - The
state
is Illinois
Write a complex condition that combines multiple tests using and
or or
in an if / else
statement to determine if you should print the temperature:
temperature = input("What is the temperature?")
year = input("What is the year?")
month = input("What is the month (1 - 12)?")
state = input("What is the state (abbreviation)?")
#### Your code below
print(temperature, year, month, state)
Create a complex condition using if / elif / else
that you may need for your own project: