8.6 Conditional Execution: Binary Selection
From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython1 class period2 exercises3 graded tests
8.6 Conditional Execution: Binary Selection
Read first in the book: 8.1 Intro: What we can do with Turtles and Conditionals, 8.2 Boolean Values and Boolean Expressions, 8.3 Logical operators, 8.4 The in and not in operators, 8.5 Precedence of Operators.
Video: watch it on YouTube.
In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. Selection statements, sometimes also referred to as conditional statements, give us this ability. The simplest form of selection is the if statement. This is sometimes referred to as binary selection since there are two possible paths of execution.
x = 15
if x % 2 == 0:
print(x, "is even")
else:
print(x, "is odd")
The syntax for an if statement looks like this:
if BOOLEAN EXPRESSION:
STATEMENTS_1 # executed if condition evaluates to True
else:
STATEMENTS_2 # executed if condition evaluates to False
The boolean expression after the if statement is called the condition.
If it is true, then the indented statements get executed. If not, then the statements
indented under the else clause get executed.
Flowchart of a if statement with an else
As with the function definition from the last chapter and other compound
statements like for, the if statement consists of a header line and a body. The header
line begins with the keyword if followed by a boolean expression and ends with
a colon (:).
The indented statements that follow are called a block. The first unindented statement marks the end of the block.
Each of the statements inside the first block of statements is executed in order if the boolean
expression evaluates to True. The entire first block of statements
is skipped if the boolean expression evaluates to False, and instead
all the statements under the else clause are executed.
There is no limit on the number of statements that can appear under the two clauses of an
if statement, but there has to be at least one statement in each block.
Check your understanding
Check your understanding: a multiple-choice question in the book.
Check your understanding: a multiple-choice question in the book.
Check your understanding: a multiple-choice question in the book.
Exercise 1
Write code to assign the string "You can apply to SI!" to output if the string "SI 106" is in the list courses. If it is not in courses, assign the value "Take SI 106!" to the variable output.
Open the
01folder and editmain.py. Run Tests checks it.
Exercise 2
Create a variable, b, and assign it the value of 15. Then, write code to see if the value b is greater than that of a. If it is, a's value should be multiplied by 2. If the value of b is less than or equal to a, nothing should happen. Finally, create variable c and assign it the value of the sum of a and b.
Open the
02folder and editmain.py. Run Tests checks it.
For teachers
8.6 Conditional Execution: Binary Selection
Students read Foundations of Python Programming's “Conditional Execution: Binary Selection” in CS Room and work through its 2 Python coding exercises (2 graded).
- Time: 1 class period
- Tests: 3 in 2 graded exercises, set up for you when you import the lesson
Reading first
- 8.1 Intro: What we can do with Turtles and Conditionals
- 8.2 Boolean Values and Boolean Expressions
- 8.3 Logical operators
- 8.4 The in and not in operators
- 8.5 Precedence of Operators
Exercises
| Folder | Grading | Tests |
|---|---|---|
01 | Autograded | 1 |
02 | Autograded | 2 |
