From fbbbff2f690276d85e50e05c6df4c68d8c1774fb Mon Sep 17 00:00:00 2001
From: Joel Chan What are conditionals and why do we care about them?A set of pictures might help to give the intuition:
Basically anytime you find yourself at a problem step or part of your problem where you say something like “do something
based on / depending on / looking at / if some condition
”, that is a signal that you need a conditional.
Let’s brainstorm some real-world examples together!
+Here are some examples:
decide what to wear: check temperature, check if i’m going to a dress code location
shopping: check how much money i have, check quality level of the thing, what i need; decide what to buy
decide what to eat: spiciness, taste, price
decide what section of a class to take
What are some other real-world examples of this sort of situation that you can think of?
We use Boolean Operators to compare TWO pieces of data. When evaluated, they yield a Boolean value (True or False).
+We use Boolean Operators to compare TWO pieces of data. When evaluated, they yield a Boolean value (True
or False
).
data1 booleanOperator data2
Here are the main ones:
We use Logical operators to combine basic Boolean expressions into more complex ones, like “is a more than 3 and less than 5”
+We use Logical operators to combine basic Boolean expressions into more complex ones, like “is a
more than 3 and less than 5”
Here are the main ones:
and
(True if all Boolean expressions are True)
Let’s practice! Translate these Boolean expressions with me from English into Python.
+Let’s practice! Translate these Boolean expressions from English into Python.
# is the driver's speed above the limit?
speed = 75
limit = 45
-speed > limit
-
True
+# boolean expression here
# do i have a passport?
hasPassport = True # assign the value True to the passport variable
-hasPassport == True
-
True
+# boolean expression here
False
+# boolean expression here
# did i take the prereq for the class OR get permissionm from the instructor?
took_prereq = False
have_permission = True
-took_prereq or have_permission
-
True
+# boolean expression here
# is the driver not wearing a seat belt?
seatBelt = False
-not seatBelt
-
True
+# boolean expression here
True
+# boolean expression here
# if my speed is above the limit, print stop; otherwise, let me pass
speed = 25
limit = 45
-if speed > limit:
- print("Stop")
-else:
- print("Go ahead")
-
Go ahead
+# conditional block below here
# if i have a passport, print come on in; otherwise, print go away
# do i have a passport?
hasPassport = False # assign the value True to the passport variable
-if hasPassport:
- print("Come on in")
-else:
- print("Go away")
-
Go away
+# conditional block below here
File "/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_61639/471952310.py", line 7
- print("Graduate")}
- ^
-SyntaxError: unmatched '}'
+# conditional block below here
Go ahead
+hello world!
+
The answer: only once! Because both print statements are scoped (indented inside) a conditional branch, it will only execute once: either in the true (if) branch or the false (else) branch.
Only stop someone if they’re above the speed limit
Tell me if someone is coming!
Look through the bag and only pull out the red skittles
Can you think of any others?
Can you think of any others?
# generic
if booleanExpression:
# do something
@@ -1060,9 +1008,12 @@ Chained conditionalsSometimes you have more than two choices of paths (branches). In that case you need an elif.
The difference from the basic conditional is something like this:
-
Simple example: you have a fever if you’re above 100, hypothermia if you’re under 95; otherwise, you’re all good!
-Or, choosing an outfit depending on where you want to go (in the Spring in Maryland!).
-Or, choosing a football play depending on what you think the defense is showing.
+Some examples:
+
you have a fever if you’re above 100, hypothermia if you’re under 95; otherwise, you’re all good!
choosing an outfit depending on where you want to go (in the Spring in Maryland!).
choosing a football play depending on what you think the defense is showing.
Any other examples?
The key difference between this type of conditional block and the regular “if/else” blocks is that you need more than one Boolean expression; one for each if
or elif
statement.
Here’s the generic structure:
@@ -1090,6 +1041,20 @@---------------------------------------------------------------------------
+NameError Traceback (most recent call last)
+/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_71416/2872807073.py in <module>
+----> 1 if gpa >= gpa_threshold and required_courses >= req_threshold:
+ 2 # then something
+ 3 print("graduate!")
+ 4 elif gpa >= gpa_threshold and required_courses < req_threshold:
+ 5 # then something else
+
+NameError: name 'gpa_threshold' is not defined
+