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 ifelif, and else branches.
  • Correctly evaluate expressions containing and and or.
  • Understand "boolean indexing" with regard to NumPy arrays.

Lecture



Alternate video link.


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 if condition to start a conditional statement, elif condition to provide additional tests, and else to provide a default.
  • The bodies of the branches of conditional statements must be indented.
  • Use == to test for equality.
  • X and Y is only true if both X and Y are True.
  • X or Y is true if either X or Y, or both, are True.
  • Zero, the empty string, and the empty list are considered false; all other numbers, strings, and lists are considered true.
  • True and False represent truth values.