Usually, statements in a program execute in the order in which they’re written. This is called sequential execution. 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 transfer of control and is achieved with Python control statements.
In the 1960s, extensive use of control transfers was causing difficulty in software development. Blame was pointed at the goto
statement. This statement allowed you to transfer control to one of many possible destinations in a program. Bohm and Jacopini’s research1 demonstrated that programs could be written without goto
statements. The notion of structured programming became almost synonymous with “goto elimination.” Python does not have a goto
statement. Structured programs are clearer, easier to debug and change, and more likely to be bug-free.
Bohm and Jacopini demonstrated that all programs could be written using three forms of control—namely, sequential execution, the selection statement and the repetition statement. Sequential execution is simple. Python statements execute one after the other “in sequence,” unless directed otherwise.
A flowchart is a graphical representation of an algorithm or a part of one. You draw flowcharts using rectangles, diamonds, rounded rectangles and small circles that you connect by arrows called flowlines. Like pseudocode, flowcharts are useful for developing and representing algorithms. They clearly show how forms of control operate. Consider the following flowchart segment, which shows sequential execution:
We use the rectangle (or action) symbol to indicate any action, such as a calculation or an input/output operation. The flowlines show the order in which the actions execute. First, the grade is added to the total, then 1 is added to the counter. We show the Python code next to each action symbol for comparison purposes. This code is not part of the flowchart.
In a flowchart for a complete algorithm, the first symbol is a rounded rectangle containing the word “Begin.” The last symbol is a rounded rectangle containing the word “End.” In a flowchart for only a part of an algorithm, we omit the rounded rectangles, instead using small circles called connector symbols. The most important symbol is the decision (or diamond) symbol, which indicates that a decision is to be made, such as in an if
statement. We begin using decision symbols in the next section.
Python provides three types of selection statements that execute code based on a condition—an expression that evaluates to either True
or False
:
The if
statement performs an action if a condition is True
or skips the action if the condition is False
.
The if…else
statement performs an action if a condition is True
or performs a differentaction if the condition is False
.
The if…elif…else
statement performs one of many different actions, depending on the truth or falsity of several conditions.
Anywhere a single action can be placed, a group of actions can be placed.
The if
statement is called a single-selection statement because it selects or ignores a single action (or group of actions). The if
…else
statement is called a double-selection statement because it selects between two different actions (or groups of actions). The if
…elif
…else
statement is called a multiple-selection statement because it selects one of many different actions (or groups of actions).
Python provides two repetition statements—while
and for
:
The while
statement repeats an action (or a group of actions) as long as a condition remains True
.
The for
statement repeats an action (or a group of actions) for every item in a sequence of items.
The words if
, elif
, else
, while
, for
, True
and False
are keywords that Python reserves to implement its features, such as control statements. Using a keyword as an identifier such as a variable name is a syntax error. The following table lists Python’s keywords.
and |
as |
assert |
async |
await |
break |
class |
continue |
def |
del |
elif |
else |
except |
False |
finally |
for |
from |
global |
if |
import |
in |
is |
lambda |
None |
nonlocal |
not |
or |
pass |
raise |
return |
True |
try |
while |
with |
yield |
You form each Python program by combining as many control statements of each type as you need for the algorithm the program implements. 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. Control-statement nesting also connects control statements—we’ll see how later in the chapter.
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.
(Fill-In) You can write all programs using three forms of control—___________ , ___________ and ___________ .
Answer: sequential execution, selection statements, repetition statements.
(Fill-In) A(n) ___________ is a graphical representation of an algorithm.
Answer: flowchart.