3.6. Summary¶
Comparison operators produce a Boolean result (type
bool
, eitherTrue
orFalse
): [More Conditional Expressions]Meaning Math Symbol Python Symbols Less than < <
Greater than > >
Less than or equal ≤ <=
Greater than or equal ≥ >=
Equals = ==
Not equal ≠ !=
Comparisons may be chained as in
a < b <= c < d != e
. [Multiple Tests and if-elif Statements]The
in
operator: [Arbitrary Types Treated As Boolean]value
in
sequenceis True if value is one of the elements in the sequence.
Operators on Boolean expressions [Compound Boolean Expressions]
- condition1
and
condition2True
only if both conditions areTrue
- condition1
or
condition2True
only if at least one condition isTrue
not
conditionTrue
only when condition isFalse
This description is sufficient if the result is used as a Boolean value (in an
if
orwhile
condition). See Arbitrary Types Treated As Boolean for the advanced use when operands are not explicitly Boolean, and the result is not going to be interpreted as Boolean.if
StatementsSimple
if
statement [Simple if Statements]if
condition:
indentedStatementBlockForTrueConditionIf the condition is
True
, then do the indented statement block. If the condition is notTrue
, then skip it.if-else
statement [if-else Statements]if
condition:
indentedStatementBlockForTrueConditionelse:
indentedStatementBlockForFalseConditionIf the condition is
True,
then do the first indented block only. If the condition is notTrue
, then skip the first indented block and do the one after theelse:
.The most general syntax for an if statement,
if
-elif
-else
[Multiple Tests and if-elif Statements]if
condition1:
indentedStatementBlockForTrueCondition1elif
condition2:
indentedStatementBlockForFirstTrueCondition2elif
condition3:
indentedStatementBlockForFirstTrueCondition3elif
condition4:
indentedStatementBlockForFirstTrueCondition4else:
indentedStatementBlockForEachConditionFalseThe
if
, eachelif
, and the finalelse
line are all aligned. There can be any number ofelif
lines, each followed by an indented block. (Three happen to be illustrated above.) With this construction exactly one of the indented blocks is executed. It is the one corresponding to the firstTrue
condition, or, if all conditions areFalse
, it is the block after the finalelse
line.if
-elif
[Multiple Tests and if-elif Statements] Theelse:
clause above may also be omitted. In that case, if none of the conditions is true, no indented block is executed.
while
statements [Simple while Loops]while
condition:
indentedStatementBlockDo the indented block if condition is
True
, and at the end of the indented block loop back and test the condition again, and continue repeating the indented block as long as the condition isTrue
after completing the indented block. Execution does not stop in the middle of the block, even if the condition becomesFalse
at that point.A
while
loop can be used to set up an (intentionally) apparently infinite loop by making condition be justTrue
. To end the loop in that case, there can be a test inside the loop that sometime becomes True, allowing the execution of areturn
statement to break out of the loop. [Fancier Animation Loop Logic (Optional)]range
function with three parameters [Simple while Loops]range(
start,
pastEnd,
step)
Return a list of elements
[
start,
start+
step,
...]
with each element step from the previous one, ending just before reaching pastEnd. If step is positive, pastEnd is larger than the last element. If step is negative, pastEnd is smaller than the last element.
Type
tuple
(
expression,
expression,
and so on)
(
expression, )
( )
- A literal tuple, with two or more elements, consists of a comma separated collection of values all enclosed in parentheses. A literal tuple with only a single element must have a comma after the element to distinguish from a regular parenthesized expression. [Loops and Tuples]
- A tuple is a kind of sequence.
- Tuples, unlike lists, are immutable (may not be altered).
Interpretation as Boolean (
True
,False
): All Python data may be converted to Boolean (typebool
). The only built-in data that have a Boolean meaning ofFalse
, in addition toFalse
itself, areNone
, numeric values equal to 0, and empty collections or sequences, like the empty list {[]}and the empty string''
. [Arbitrary Types Treated As Boolean]Additional programming techniques
These techniques extend the techniques listed in the summary of the previous chapter. [Summary]
The basic pattern for programming with a while loop is [Simple while Loops]
initializationwhile
continuationCondition:
main action to repeatprepare variables for next time through loopInteractive while loops generally follow the pattern [Interactive while Loops]
input first data from userwhile
continationConditionBasedOnTestOfUserData:
process user datainput next user dataOften the code to input the first data and the later data is the same, but it must appear in both places!
Sentinel Loops [Interactive while Loops]
Often the end of the repetition of a data-reading loop is indicated by a sentinel in the data: a data value known to both the user and the program to not be regular data, that is specifically used to signal the end of the data.
Nesting Control Flow Statements [Nesting Control-Flow Statements]
If
statements may be nested inside loops, so the loop does not have to execute all the same code each time; it just needs to start with the same test.- Loops may be nested. The inner loop completes its repetitions each time before going back to the outer loop heading.
Breaking a repeating pattern into a loop [Graphical Applications]
Since a loop is basically circular, there may be several choices of where to split it to list it in the loop body. The split point needs to be where the continuation test is ready to be run, but that may still allow flexibility. When you choose to change the starting point of the loop, and rotate statements between the beginning and the end of the loop, you change what statements need to be included before and after the loop, sometimes repeating or undoing actions taken in the loop.
Tuples in lists [Loops and Tuples] A list may contain tuples. A
for
-each loop may process tuples in a list, and thefor
loop heading can do multiple assignments to variables for each element of the next tuple.Tuples as return values [Loops and Tuples] A function may return more than one value by wrapping them in a tuple. The function may then be used in a multiple assignment statement to extract each of the returned variables.
Graphics
- Zelle’s Graphics GraphWin method
checkMouse()
allows mouse tests without stopping animation, by testing the last mouse click, not waiting for a new one. [Graphical Applications] - The most finished examples of using Zelle’s graphics are in [Loops and Tuples] and [Graphical Applications].
- Zelle’s Graphics GraphWin method