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
.
if
statement is a decision point in a program; it determines what will execute next. (Alternative link)
OR read 5.1 - 5.7 AND watch the last three lesson videos in the list above.
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).
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.