4. More Control Flow Tools¶
As well as the while statement just introduced, Python uses a few more
that we will encounter in this chapter.
4.1. if Statements¶
Perhaps the most well-known statement type is the if statement. For
example:
>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
... x = 0
... print('Negative changed to zero')
... elif x == 0:
... print('Zero')
... elif x == 1:
... print('Single')
... else:
... print('More')
...
More
There can be zero or more elif parts, and the else part is
optional. The keyword ‘elif’ is short for ‘else if’, and is useful
to avoid excessive indentation. An if … elif …
elif … sequence is a substitute for the switch or
case statements found in other languages.
If you’re comparing the same value to several constants, or checking for specific types or
attributes, you may also find the match statement useful. For more
details see match Statements.
4.2. for Statements¶
The for statement in Python differs a bit from what you may be used
to in C or Pascal. Rather than always iterating over an arithmetic progression
of numbers (like in Pascal), or giving the user the ability to define both the
iteration step and halting condition (as C), Python’s for statement
iterates over the items of any sequence (a list or a string), in the order that
they appear in the sequence. For example (no pun intended):
>>> # Measure some strings:
>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words:
... print(w, len(w))
...
cat 3
window 6
defenestrate 12
Code that modifies a collection while iterating over that same collection can be tricky to get right. Instead, it is usually more straight-forward to loop over a copy of the collection or to create a new collection:
# Create a sample collection
users = {'Hans': 'active', 'Éléonore': 'inactive', '景太郎': 'active'}
# Strategy: Iterate over a copy
for user, status in users.copy().items():
if status == 'inactive':
del users[user]
# Strategy: Create a new collection
active_users = {}
for user, status in users.items():
if status == 'active':
active_users[user] = status
4.3. The range() Function¶
If you do need to iterate over a sequence of numbers, the built-in function
range() comes in handy. It generates arithmetic progressions:
>>> for i in range(5):
... print(i)
...
0
1
2
3
4
The given end point is never part of the generated sequence; range(10) generates
10 values, the legal indices for items of a sequence of length 10. It
is possible to let the range start at another number, or to specify a different
increment (even negative; sometimes this is called the ‘step’):
>>> list(range(5, 10))
[5, 6, 7, 8, 9]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(-10, -100, -30))
[-10, -40, -70]
To iterate over the indices of a sequence, you can combine range() and
len() as follows:
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
... print(i, a[i])
...
0 Mary
1 had
2 a
3 little
4 lamb
In most such cases, however, it is convenient to use the enumerate()
function, see Looping Techniques.
A strange thing happens if you just print a range:
>>> range(10)
range(0, 10)
In many ways the object returned by range() behaves as if it is a list,
but in fact it isn’t. It is an object which returns the successive items of
the desired sequence when you iterate over it, but it doesn’t really make
the list, thus saving space.
We say such an object is iterable, that is, suitable as a target for
functions and constructs that expect something from which they can
obtain successive items until the supply is exhausted. We have seen that
the for statement is such a construct, while an example of a function
that takes an iterable is sum():
>>> sum(range(4)) # 0 + 1 + 2 + 3
6
Later we will see more functions that return iterables and take iterables as
arguments. In chapter Data Structures, we will discuss list() in more
detail.
4.4. break and continue Statements¶
The break statement breaks out of the innermost enclosing
for or while loop:
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print(f"{n} equals {x} * {n//x}")
... break
...
4 equals 2 * 2
6 equals 2 * 3
8 equals 2 * 4
9 equals 3 * 3
The continue statement continues with the next
iteration of the loop:
>>> for num in range(2, 10):
... if num % 2 == 0:
... print(f"Found an even number {num}")
... continue
... print(f"Found an odd number {num}")
...
Found an even number 2
Found an odd number 3
Found an even number 4
Found an odd number 5
Found an even number 6
Found an odd number 7
Found an even number 8
Found an odd number 9
4.5. else Clauses on Loops¶
In a for or while loop the break statement
may be paired with an else clause. If the loop finishes without
executing the break, the else clause executes.
In a for loop, the else clause is executed
after the loop finishes its final iteration, that is, if no break occurred.
In a while loop, it’s executed after the loop’s condition becomes false.
In either kind of loop, the else clause is not executed if the
loop was terminated by a break. Of course, other ways of ending the
loop early, such as a return or a raised exception, will also skip
execution of the else clause.
This is exemplified in the following for loop,
which searches for prime numbers:
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print(n, 'equals', x, '*', n//x)
... break
... else:
... # loop fell through without finding a factor
... print(n, 'is a prime number')
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
(Yes, this is the correct code. Look closely: the else clause belongs to
the for loop, not the if statement.)
One way to think of the else clause is to imagine it paired with the if
inside the loop. As the loop executes, it will run a sequence like
if/if/if/else. The if is inside the loop, encountered a number of times. If
the condition is ever true, a break will happen. If the condition is never
true, the else clause outside the loop will execute.
When used with a loop, the else clause has more in common with the else
clause of a try statement than it does with that of if
statements: a try statement’s else clause runs when no exception
occurs, and a loop’s else clause runs when no break occurs. For more on
the try statement and exceptions, see Handling Exceptions.
4.6. pass Statements¶
The pass statement does nothing. It can be used when a statement is
required syntactically but the program requires no action. For example:
>>> while True:
... pass # Busy-wait for keyboard interrupt (Ctrl+C)
...
This is commonly used for creating minimal classes:
>>> class MyEmptyClass:
... pass
...
Another place pass can be used is as a place-holder for a function or
conditional body when you are working on new code, allowing you to keep thinking
at a more abstract level. The pass is silently ignored:
>>> def initlog(*args):
... pass # Remember to implement this!
...
For this last case, many people use the ellipsis literal ... instead of
pass. This use has no special meaning to Python, and is not part of
the language definition (you could use any constant expression here), but
... is used conventionally as a placeholder body as well.
See The Ellipsis Object.
4.7. match Statements¶
A match statement takes an expression and compares its value to successive
patterns given as one or more case blocks. This is superficially
similar to a switch statement in C, Java or JavaScript (and many
other languages), but it’s more similar to pattern matching in
languages like Rust or Haskell. Only the first pattern that matches
gets executed and it can also extract components (sequence elements
or object attributes) from the value into variables. If no case matches,
none of the branches is executed.
The simplest form compares a subject value against one or more literals:
def http_error(status):
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case 418:
return "I'm a teapot"
case _:
return "Something's wrong with the internet"
Note the last bl
评论
0 条评论
请登录写评论。