Making Choices - Lesson 5
Questions
- How can my programs do different things based on data values?
Objectives
- Understand "expressions" and booleans.
- Write conditional statements including
if,elif, andelsebranches. - Correctly evaluate expressions containing
andandor. - Understand "boolean indexing" with regard to NumPy arrays.
Lecture
Jupyter Notebook
Please use the Jupyter notebook named "05MakingChoices.ipynb" for this lesson. If you haven't downloaded them already, you may find them here.
Key Points
- Use
ifcondition to start a conditional statement,elifcondition to provide additional tests, andelseto provide a default. - The bodies of the branches of conditional statements must be indented.
- Use
==to test for equality. X and Yis only true if bothXandYareTrue.X or Yis true if eitherXorY, or both, areTrue.- Zero, the empty string, and the empty list are considered false; all other numbers, strings, and lists are considered true.
TrueandFalserepresent truth values.
