Conditional statements allow decisions to be made during program execution: will some group of statements be executed or will they be skipped? Conditional statements are also called branching statements because program execution can "branch off" on different paths. The keywords you will learn are if, else, and elif.

Lessons - watch before lecture

  1. Program Flow (3 minutes) (Alternative link)
  2. If Statements (4 minutes) An if statement is a decision point in a program; it determines what will execute next. (Alternative link)
  3. If-Else Statements (3 minutes) This is a decision point where a program chooses one option out of two possibilities. (Alternative link)
  4. If-Elif Statements (5 minutes) This is a decision point where there are multiple options. (Alternative link)
  5. Conditionals and Call Frames (5 minutes) (Alternative link)
  6. Conditionals and Test Cases (3 minutes) (Alternative link)
  7. Tracing Conditionals (5 minutes) Conditionals can complicate debugging. This video references the file traces.py. (Alternative link)

OR read 5.1 - 5.7 AND watch the last three lesson videos in the list above.


Lecture materials

Slides: individual slides for viewing, 6-up layout for printing

Example: quadrants.py (skeleton)
To download the above .py files, right-click (Windows) or command-click (Macs) on the filename and then select "Save link as".
This way you choose where the files will be saved, instead of having your computer save to the default folder (from which you will later have to move your files for organization).

Lecture Recording

Answers to the in-lecture questions: See the lecture slides file above for the questions. After solving the problems yourself, you can check your answers here.

Practice: Complete the function whichQuad_n in the quadrants module using nested conditionals, corresponding to lecture slides 48 and 49. Do not use elif. We will discuss this function next lecture.