Chapter 3: Control Statements and Program Development 1
Control Statements and Program
3.1 Introduction
3.2 Algorithms
3.2 Q1: Which of the following statements a), b) or c) is false?
a. You can solve any computing problem by executing a series of actions in a spe-
cific order.
b. An algorithm is a procedure for solving a problem in terms of the actions to
execute, and the order in which these actions execute.
c. Program control specifies the order in which statements (actions) execute in a
program.
d. All of the above statements are true.
3.3 Pseudocode
3.3 Q1: ________ is an informal English-like language for “thinking out” algorithms.
a. Code
b. Pseudocode
c. Python
d. Quasicode
3.4 Control Statements
3.4 Q1: Various Python statements enable you to specify that the next statement
to execute may be other than the next one in sequence. This is called ________ and
is achieved with Python control statements.
a. transfer of control
b. alternate execution
c. spread execution
2 Chapter 3: Control Statements and Program Development
d. None of the above
3.4 Q2: The most important flowchart symbol is the ________, which indicates that
a decision is to be made, such as in an if statement.
a. rectangle
b. flowline
c. diamond
d. small circle
3.4 Q3: Which of the following statements is false?
a. Python provides three types of selection statements that execute code based on
a condition—any expression that evaluates to either True or False.
b. The if…else statement performs an action if a condition is True or per–
forms a different action if the condition is False.
c. Anywhere a single action can be placed, a group of actions can be placed.
d. The if…elif…else statement is called a double-selection statement because
it selects one of two different actions (or groups of actions).
3.4 Q4: Python provides two iteration statements—________ and ________:
a. do while and for
b. while and for each
c. do while and for each
d. while and for
3.4 Q5: Which of the following statements a), b) or c) is false?
a. You form each Python program by combining as many control statements of
each type as you need for the algorithm the program implements.
b. With Single-entry/single-exit (one way in/one way out) control statements, the
exit point of one connects to the entry point of the next. This is similar to the way
a child stacks building blocks—hence, the term control-statement stacking.
c. You can construct any Python program from only six different forms of control
(sequential execution, and the if, if…else, if…elif…else, while and for
statements). You combine these in only two ways (control-statement stacking
and control-statement nesting). This is the essence of simplicity.
Chapter 3: Control Statements and Program Development 3
d. All of the above statements are true.
3.5 if Statement
3.5 Q1: Which of the following statements is false?
a. Indenting a suite is required; otherwise, an IndentationError syntax error
occurs.
b. If you have more than one statement in a suite, those statements do not need
to have the same indentation.
c. Sometimes error messages may not be clear. The fact that Python calls attention
to the line is often enough for you to figure out what’s wrong.
d. Programs that are not uniformly indented are hard to read.
3.5 Q2: Which of the following statements is false?
a. The decision (diamond) symbol contains a condition that can be either True or
False.
b. The diamond flowchart symbol has three flowlines emerging from it.
c. One flowline emerging from the diamond flowchart symbol indicates the direc-
tion to follow when the condition in the symbol is True. This points to the action
(or group of actions) that should execute.
d. Another flowline emerging from the diamond flowchart symbol indicates the
direction to follow when the condition is False. This skips the action (or group
of actions).
3.5 Q3: Which of the following statements is false?
a. Any expression may be evaluated as True or False.
b. A condition which evaluates to a nonzero value is considered to be True, and a
condition which evaluates to a value of zero is considered to be False.
c. Strings containing characters are True and empty strings (”, “” or “”””””)
are False.
d. All of the above statements are true.
6 Chapter 3: Control Statements and Program Development
© Copyright 2020 by Pearson Education, Inc. All Rights Reserved.
Answer: b. Actually, end is not a Python keyword.
3.8 Q2: What does the following line of code display?
print(10, 20, 30, sep=‘, ‘)
a. 102030
b. 10,20,30
c. 10 20 30
d. 10, 20, 30
3.8.1 Iterables, Lists and Iterators
3.8 Q3: Which of the following statements is false?
a. The sequence to the right of the for statement’s keyword in must be an itera-
ble.
b. An iterable is an object from which the for statement can take one item at a
time until no more items remain.
c. One of Python’s most common iterable sequences is the list, which is a comma–
separated collection of items enclosed in square brackets ([ and ]).
d. The following code totals five integers in a list:
total = 0
for number in [2, -3, 0, 17, 9]:
total + number
3.8 Q4: Which of the following statements is false?
a. Each sequence has an iterator.
b. The for statement uses the iterator “behind the scenes” to get each consecutive
item until there are no more to process.
c. The iterator is like a bookmark—it always knows where it is in the sequence,
so it can return the next item when it’s called upon to do so.
d. Lists are unordered and a list’s items are mutable.
10 Chapter 3: Control Statements and Program Development
# process 10 students
for student in range(10):
# get one exam result
result = int(input(‘Enter result (1=pass, 2=fail): ‘))
if result == 1:
passes = passes + 1
else:
failures = failures + 1
which of the following statements is true?
a. The if statement is nested in the for statement.
b. The if statement follows the for statement in sequence.
c. The for statement is nested in the if statement.
d. None of the above.
3.13 Built-In Function range: A Deeper Look
3.13 Q1: Which of the following statements is false?
a. Function range’s one-argument version produces a sequence of consecutive
integers from 0 up to, but not including, the argument’s value.
b. The following snippet produces the sequence 5 6 7 8 9.
for number in range(5, 10):
print(number, end=‘ ‘)
c. The following snippet produces the sequence 0 2 4 6 8.
for number in range(0, 10, 2):
print(number, end=‘ ‘)
d. The following snippet produces the sequence 10 8 6 4 2 0.
for number in range(10, 0, -2):
print(number, end=‘ ‘)
3.14 Using Type Decimal for Monetary Amounts
3.14 Q1: Which of the following statements is false?
a. Many applications require precise representation of numbers with decimal
points.
Chapter 3: Control Statements and Program Development 11
b. Financial institutions like banks that deal with millions or even billions of trans-
actions per day have to tie out their transactions “to the penny.” Floating-point
numbers can represent some but not all monetary amounts with to-the-penny
precision.
c. For monetary calculations and other applications that require precise repre-
sentation and manipulation of numbers with decimal points, the Python Standard
Library provides type Decimal, which uses a special coding scheme to solve the
problem of “to-the-penny precision.” Banks also have to deal with other issues
such as using a fair rounding algorithm when they’re calculating daily interest on
accounts. Type Decimal offers such capabilities.
d. Floating-point values are stored and represented precisely in binary format.
3.14 Q2: Which of the following statements a), b) or c) is false?
a. The Python Standard Library is divided into modules—groups of related capa-
bilities.
b. The decimal module defines type Decimal and its capabilities.
c. To use Decimal, you must first import its module, as in
import decimal
and refer to the Decimal type as decimal.Decimal, or you must indicate a spe-
cific capability to import using from…import, as in:
from decimal import Decimal
which imports only the type Decimal from the decimal module so that you can
use it in your code.
d. All of the above statements are true.
3.14 Q3: Which of the following statements is false?
a. You typically create a Decimal from a string.
b. Decimals support the standard arithmetic operators +, –, *, /, //, ** and %, as
well as the corresponding augmented assignments.
c. You may perform arithmetic between Decimals and integers.
d. You may perform arithmetic between Decimals and floating-point numbers.
3.14 Q4: Which of the following statements is false?
12 Chapter 3: Control Statements and Program Development
a. The following statement uses an f-string with two placeholders to format year
and amount:
print(f‘{year:>2}{amount:>10.2f}’)
b. The placeholder {year:>2} uses the format specifier >2 to indicate that year’s
value should be right aligned (>) in a field of width 2—the field width specifies
the number of character positions to use when displaying the value.
c. For single-digit year values 1–9, the format specifier >2 displays a value fol-
lowed by the space character, thus right aligning the years in the first column.
d. The format specifier 10.2f in the placeholder {amount:>10.2f} formats
amount as a floating-point number (f) right aligned (>) in a field width of 10 with
a decimal point and two digits to the right of the decimal point (.2). Formatting
a column of amounts this way aligns their decimal points vertically, as is typical
with monetary amounts.
3.15 break and continue Statements
3.15 Q1: Which of the following statements is false?
a. Executing a break statement in a while or for immediately exits that state-
ment.
b. The following snippet produces the integer sequence 0 1 2 3 4 5 6 7 8 9:
for number in range(100):
if number == 10:
break
print(number, end=‘ ‘)
c. The while and for statements each have an optional else clause that executes
only if the loop terminates normally.
d. The following code snippet produces the sequence 0 1 2 3 4 5 5 6 7 8 9:
for number in range(10):
if number == 5:
continue
print(number, end=‘ ‘)
14 Chapter 3: Control Statements and Program Development
© Copyright 2020 by Pearson Education, Inc. All Rights Reserved.
Answer: d. Actually, in operator expressions that use the and operator,
make the condition that’s more likely to be False the leftmost condition—
in or operator expressions, make the condition that’s more likely to be True
the leftmost condition—each of these tactics can reduce a program’s execu-
tion time.
3.16 Q3: Which of the following statements is false?
a. You place the not operator before a condition to choose a path of execution if
the original condition (without the not operator) is True.
b. The if statement
if not grade == -1:
print(‘The next grade is’, grade)
also can be written as follows:
if grade != -1:
print(‘The next grade is’, grade)
c. The Boolean not operator reverses the meaning of a condition—True becomes
False and False becomes True.
d. The not operator is a unary operator—it has only one operand.
3.17 Intro to Data Science: Measures of Central
3.17 Q1: Which of the following statements is false?
a. The descriptive statistics mean, median and mode are measures of central ten-
dency—each is a way of producing a single value that is in some sense typical of
the others.
b. The following session creates a list called grades, then uses the built-in sum
and len functions to calculate the median “by hand”—sum calculates the total of
the grades (397) and len returns the number of grades (5):
In [1]: grades = [85, 93, 45, 89, 85]
In [2]: sum(grades) / len(grades)
Out[2]: 79.4
Chapter 3: Control Statements and Program Development 15
c. Like functions min and max, sum and len are both examples of functional-style
programming reductions—they reduce a collection of values to a single value.
d. The Python Standard Library’s statistics module provides functions for cal-
culating the mean, median and mode—these, too, are reductions.
3.17 Q2: Which of the following statements is false?
a. The argument of each of the statistics module’s mean, median and mode
functions must be an iterable.
b. To help confirm the median and mode values of a grades list, you can use the
built-in sorted function to get a copy of grades with its values arranged in in-
creasing order, as in the following session, which makes it clear that both the me-
dian and the mode are 85:
In [1]: grades = [85, 93, 45, 89, 85]
In [2]: sorted(grades)
Out[2]: [45, 85, 85, 89, 93]
c. If a list’s number of values is even, median returns the mode of the two middle
values.
d. The mode function causes a StatisticsError for lists like
[85, 93, 45, 89, 85, 93]
in which there are two or more “most frequent” values. Such a set of values is said
to be bimodal.