Conditionals Preparation
Overview
Frequently you want a program to do one thing in one situation and a different thing some other time. The programming structure that allows for this is a conditional. Today will be focused on the conditional structures in Python and the necessary Boolean values.
Basic Learning Objectives
Before class, you should be able to:
- Define what Boolean values are
- Recognize the
if/elif/elsesyntax for conditionals - Define what the
and,or, andnotlogical operators do - Define what the
inoperator does in the basic case
Advanced Learning Objectives
After class, you should be able to:
- Use basic, nested, and chained conditionals to solve a problem
- Use logical operators to solve a problem
- Predict what code will do when short-circuit boolean evaluation is used
Resources
You should read/watch the following:
- 5.1 What we can do with Conditionals
- 5.2 Boolean Values and Expressions
- 5.3 Logical operators
- 5.4 The
inandnot inopreators - 5.6 Conditional Execution
- 5.7 Omiting the
elseClause - 5.8 Nested conditionals
- 5.9 Chained conditionals
Checks
Submit answers to the following on Moodle.
- (Checkpoint 5.9.2) What will the following code print?
x = 3
y = 5
z = 2
if x < y and x < z:
print("a")
elif y < x and y < z:
print("b")
else:
print("c")
- Write a snippet of code that prints out whether the value in the variable
mysteryis greater than or less than 5.