Text
stringlengths
1
9.41k
Continuation lines (lines 2 and beyond of the statement) can start at any indentation level you like, but you should try to make them align vertically for readability if possible.
This open pairs rule also covers set and dictionary comprehensions in Python 3.0. - Statements may span multiple lines if they end in a backslash.
This is a somewhat outdated feature, but if a statement needs to span multiple lines, you can also add a backslash (a \ not embedded in a string literal or comment) at the end of the prior line to indicate you’re continuing on the next line.
Because you can also continue by adding parentheses around most constructs, backslashes are almost never used.
This approach is error-prone: accidentally forgetting a \ usually generates a syntax error and might even cause the next line to be silently mistaken to be a new statement, with unexpected results. **|** ----- - Special rules for string literals.
As we learned in Chapter 7, triple-quoted string blocks are designed to span multiple lines normally.
We also learned in Chapter 7 that adjacent string literals are implicitly concatenated; when used in conjunction with the open pairs rule mentioned earlier, wrapping this construct in parentheses allows it to span multiple lines. - Other rules.
There are a few other points to mention with regard to statement delimiters.
Although uncommon, you can terminate a statement with a semicolon—this convention is sometimes used to squeeze more than one simple (noncompound) statement onto a single line.
Also, comments and blank lines can appear anywhere in a file; comments (which begin with a # character) terminate at the end of the line on which they appear. ###### A Few Special Cases Here’s what a continuation line looks like using the open syntactic pairs rule.
Delimited constructs, such as lists in square brackets, can span across any number of lines: ``` L = ["Good", "Bad", "Ugly"] # Open pairs may span lines ``` This also works for anything in parentheses (expressions, function arguments, function headers, tuples, and generator expressions), as well ...
Some of these are tools we’ll study in later chapters, but this rule naturally covers most constructs that span lines in practice. If you like using backslashes to continue lines, you can, but it’s not common practice in Python: ``` if a == b and c == d and \ d == e and f == g: print('olde') # Backs...
In the following, x is assigned 10 with the backslash, as intended; if the backslash is accidentally omitted, though, x is assigned 6 instead, and no error is reported (the +4 is a valid expression statement by itself). **|** **f** ----- In a real program with a more complex assignment, this could be the source of ...
Some coders use this form to save program file real estate, but it usually makes for more readable code if you stick to one statement per line for most of your work: ``` x = 1; y = 2; print(x) # More than one simple statement ``` As we learned in Chapter 7, triple-quoted string literals span lines too.
In addition, if two string literals appear next to each other, they are concatenated as if a + had been added between them—when used in conjunction with the open pairs rule, wrapping in parentheses allows this form to span multiple lines.
For example, the first of the following inserts newline characters at line breaks and assigns `S to` `'\naaaa\nbbbb` ``` \ncccc', and the second implicitly concatenates and assigns S to 'aaaabbbbcccc'; com ``` ments are ignored in the second form, but included in the string in the first: ``` S = """ aaaa bbbb c...
You’ll most often see this used for simple if statements with a single test and action: ``` if 1: print('hello') # Simple statement on header line ``` You can combine some of these special cases to write code that is difficult to read, but I don’t recommend it; as a rule of thumb, try to keep each statement on...
Six months down the road, you’ll be happy you did. - Frankly, it’s surprising that this wasn’t removed in Python 3.0, given some of its other changes!
(See Table P-2 of the Preface for a list of 3.0 removals; some seem fairly innocuous in comparison with the dangers inherent in backslash continuations.) Then again, this book’s goal is Python instruction, not populist outrage, so the best advice I can give is simply: don’t do this. **|** ----- ###### Truth Tests ...
In particular, Python’s Boolean operators are a bit different from their counterparts in languages like C.
In Python: - Any nonzero number or nonempty object is true. - Zero numbers, empty objects, and the special object None are considered false. - Comparisons and equality tests are applied recursively to data structures. - Comparisons and equality tests return True or False (custom versions of 1 and 0). - Boolea...
There are three Boolean expression operators in Python: ``` X and Y ``` Is true if both X and Y are true ``` X or Y ``` Is true if either X or Y is true ``` not X ``` Is true if X is false (the expression returns True or False) Here, X and Y may be any truth value, or any expression that returns a truth value (e.g....
Boolean operators are typed out as words in Python (instead of C’s &&, ||, and !). Also, Boolean and and or operators return a true or false object in Python, not the values True or False.
Let’s look at a few examples to see how this works: `>>> 2 < 3, 3 < 2` _# Less-than: return True or False (1 or 0)_ ``` (True, False) ``` Magnitude comparisons such as these return True or False as their truth results, which, as we learned in Chapters 5 and 9, are really just custom versions of the integers 1 and `...
If we test their results in if or other statements, they will be as expected (remember, every object is inherently true or false), but we won’t get back a simple True or False. **|** **f** ----- For or tests, Python evaluates the operand objects from left to right and returns the first one that is true.
Moreover, Python stops at the first true operand it finds.
This is usually called short-circuit evaluation, as determining a result short-circuits (terminates) the rest of the expression: `>>> 2 or 3, 3 or 2` _# Return left operand if true_ ``` (2, 3) # Else, return right operand (true or false) >>> [] or 3 3 >>> [] or {} {} ``` In the first line of the pre...
In the other two tests, the left operand is false (an empty object), so Python simply evaluates and returns the object on the right (which may happen to have either a true or a false value when tested). ``` and operations also stop as soon as the result is known; however, in this case Python ``` evaluates the operands...
In the second test, the left operand is false ([]), so Python stops and returns it as the test result.
In the last test, the left side is true (3), so Python evaluates and returns the object on the right (which happens to be a false []). The end result of all this is the same as in C and most other languages—you get a value that is logically true or false if tested in an if or while.
However, in Python Booleans return either the left or the right object, not a simple integer flag. This behavior of and and or may seem esoteric at first glance, but see this chapter’s sidebar “Why You Will Care: Booleans” on page 323 for examples of how it is sometimes used to advantage in coding by Python programmer...
The next section also shows a common way to leverage this behavior, and its replacement in more recent versions of Python. ###### The if/else Ternary Expression One common role for the prior section’s Boolean operators is to code an expression that runs the same as an if statement.
Consider the following statement, which sets ``` A to either Y or Z, based on the truth value of X: ``` **f** **|** ----- ``` if X: A = Y else: A = Z ``` Sometimes, though, the items involved in such a statement are so simple that it seems like overkill to spread them across four lines.
At other times, we may want to nest such a construct in a larger statement instead of assigning its result to a variable.
For these reasons (and, frankly, because the C language has a similar tool[†]), Python 2.5 introduced a new expression format that allows us to say the same thing in one expression: ``` A = Y if X else Z ``` This expression has the exact same effect as the preceding four-line if statement, but it’s simpler to code.
As in the statement equivalent, Python runs expression Y only if ``` X turns out to be true, and runs expression Z only if X turns out to be false.
That is, it ``` _short-circuits, just like the Boolean operators described in the prior section.
Here are_ some examples of it in action: `>>> A = 't' if 'spam' else 'f'` _# Nonempty is true_ ``` >>> A 't' >>> A = 't' if '' else 'f' >>> A 'f' ``` Prior to Python 2.5 (and after 2.5, if you insist), the same effect can often be achieved by a careful combination of the and and or operators, because they r...
If that is the case, the effect is the same: the and runs first and returns Y if X is true; if it’s not, the or simply returns Z.
In other words, we get “if X then Y else Z.” This and/or combination also seems to require a “moment of great clarity” to understand the first time you see it, and it’s no longer required as of 2.5—use the equivalent and more robust and mnemonic Y if X else Z instead if you need this as an expression, or use a full if...
X : Z. This was reportedly done in response to analysis of common use patterns in Python code. According to rumor, this order was also chosen in part to discourage ex-C programmers from overusing it!
Remember, simple is better than complex, in Python and elsewhere. **|** **f** ----- For example: ``` >>> ['f', 't'][bool('')] 'f' >>> ['f', 't'][bool('spam')] 't' ``` However, this isn’t exactly the same, because Python will not short-circuit—it will always run both Z and Y, regardless of the value of X.
Because of such complexities, you’re better off using the simpler and more easily understood if/else expression as of Python 2.5 and later.
Again, though, you should use even that sparingly, and only if its parts are all fairly simple; otherwise, you’re better off coding the full if statement form to make changes easier in the future.
Your coworkers will be happy you did. Still, you may see the and/or version in code written prior to 2.5 (and in code written by C programmers who haven’t quite let go of their dark coding pasts...). ###### Why You Will Care: Booleans One common way to use the somewhat unusual behavior of Python Boolean operators is...
A statement such as this: ``` X = A or B or C or None ``` sets X to the first nonempty (that is, true) object among A, B, and C, or to None if all of them are empty.
This works because the or operator returns one of its two objects, and it turns out to be a fairly common coding paradigm in Python: to select a nonempty object from among a fixed-size set, simply string them together in an or expression.
In simpler form, this is also commonly used to designate a default—the following sets X to A if A is true (or nonempty), and to default otherwise: ``` X = A or default ``` It’s also important to understand short-circuit evaluation because expressions on the right of a Boolean operator might call functions that per...
To guarantee that both functions will be run, call them before the or: ``` tmp1, tmp2 = f1(), f2() if tmp1 or tmp2: ... ``` You’ve already seen another application of this behavior in this chapter: because of the way Booleans work, the expression ((A and B) or C) can be used to emulate an if/ ``` else stateme...
As we saw in Chapter 9, because all objects are inherently true or false, it’s common and easier in Python to test an object directly (if X:) than to compare it to an empty value (if X != '':).
For a string, the two tests are equivalent.
As we also saw in Chapter 5, the preset Booleans values True and False are the same as the integers 1 and 0 and are useful for initializing variables **f** **|** ----- ###### Chapter Summary In this chapter, we studied the Python if statement.
Additionally, because this was our first compound and logical statement, we reviewed Python’s general syntax rules and explored the operation of truth tests in more depth than we were able to previously. Along the way, we also looked at how to code multiway branching in Python and learned about the if/else expression i...
There, we’ll learn about alternative ways to code loops in Python, ``` some of which may be better than others.
Before that, though, here is the usual chapter quiz. ###### Test Your Knowledge: Quiz 1. How might you code a multiway branch in Python? 2.
How can you code an if/else statement as an expression in Python? 3. How can you make a single statement span many lines? 4.
What do the words True and False mean? ###### Test Your Knowledge: Answers 1.
An if statement with multiple elif clauses is often the most straightforward way to code a multiway branch, though not necessarily the most concise.
Dictionary indexing can often achieve the same result, especially if the dictionary contains callable functions coded with def statements or lambda expressions. 2.
In Python 2.5 and later, the expression form Y if X else Z returns Y if X is true, or ``` Z otherwise; it’s the same as a four-line if statement.
The and/or combination ``` (((X and Y) or Z)) can work the same way, but it’s more obscure and requires that the Y part be true. **|** **f** ----- 3.
Wrap up the statement in an open syntactic pair ((), [], or {}), and it can span as many lines as you like; the statement ends when Python sees the closing (right) half of the pair, and lines 2 and beyond of the statement can begin at any indentation level. 4.
True and False are just custom versions of the integers 1 and 0, respectively: they always stand for Boolean true and false values in Python.
They’re available for use in truth tests and variable initialization and are printed for expression results at the interactive prompt. **|** ----- ----- ###### CHAPTER 13 ### while and for Loops This chapter concludes our tour of Python procedural statements by presenting the language’s two main looping construct...
The first of these, the while statement, provides a way to code general loops.
The second, the for statement, is designed for stepping through the items in a sequence object and running a block of code for each. We’ve seen both of these informally already, but we’ll fill in additional usage details here.
While we’re at it, we’ll also study a few less prominent statements used within loops, such as break and continue, and cover some built-ins commonly used with loops, such as range, zip, and map. Although the while and for statements covered here are the primary syntax provided for coding repeated actions, there are ad...
Because of that, the iteration story is continued in the next chapter, where we’ll explore the related ideas of Python’s iteration protocol (used by the for loop) and list _comprehensions (a close cousin to the for loop).
Later chapters explore even more exotic_ iteration tools such as generators, filter, and reduce.
For now, though, let’s keep things simple. ###### while Loops Python’s while statement is the most general iteration construct in the language.
In simple terms, it repeatedly executes a block of (normally indented) statements as long as a test at the top keeps evaluating to a true value.
It is called a “loop” because control keeps looping back to the start of the statement until the test becomes false.
When the test becomes false, control passes to the statement that follows the while block.
The net effect is that the loop’s body is executed repeatedly while the test at the top is true; if the test is false to begin with, the body never runs. ----- ###### General Format In its most complex form, the `while statement consists of a header line with a test` expression, a body of one or more indented state...
Python keeps evaluating the test at the top and executing the statements nested in the loop body until the test returns a false value: ``` while <test>: # Loop test <statements1> # Loop body else: # Optional else <statements2> # Run if didn't exit loop with break ###### ...
The first, which consists of a print statement nested in a while loop, just prints a message forever.
Recall that ``` True is just a custom version of the integer 1 and always stands for a Boolean true value; ``` because the test is always true, Python keeps executing the body forever, or until you stop its execution.
This sort of behavior is usually called an infinite loop: ``` >>> while True: ...
print('Type Ctrl-C to stop me!') ``` The next example keeps slicing off the first character of a string until the string is empty and hence false.
It’s typical to test an object directly like this instead of using the more verbose equivalent (while x != '':).
Later in this chapter, we’ll see other ways to step more directly through the items in a string with a for loop. ``` >>> x = 'spam' ``` `>>> while x:` _# While x is not empty_ ``` ...
print(x, end=' ') ``` `...
x = x[1:]` _# Strip first character off x_ ``` ... spam pam am m ``` Note the end=' ' keyword argument used here to place all outputs on the same line separated by a space; see Chapter 11 if you’ve forgotten why this works as it does.
The following code counts from the value of a up to, but not including, b.
We’ll see an easier way to do this with a Python for loop and the built-in range function later: ``` >>> a=0; b=10 ``` `>>> while a < b:` _# One way to code counter loops_ ``` ...
print(a, end=' ') ``` `... a += 1` _# Or, a = a + 1_ ``` ... 0 1 2 3 4 5 6 7 8 9 ``` Finally, notice that Python doesn’t have what some languages call a “do until” loop statement.
However, we can simulate one with a test and break at the bottom of the loop body: **|** **f** ----- ``` while True: ...loop body... if exitTest(): break ``` To fully understand how this structure works, we need to move on to the next section and learn more about the break statement. ###### break, conti...
While we’re looking at oddballs, we will also study the loop ``` else clause here, because it is intertwined with break, and Python’s empty placeholder ``` statement, the pass (which is not tied to loops per se, but falls into the general category of simple one-word statements).
In Python: ``` break ``` Jumps out of the closest enclosing loop (past the entire loop statement) ``` continue ``` Jumps to the top of the closest enclosing loop (to the loop’s header line) ``` pass ``` Does nothing at all: it’s an empty statement placeholder ``` Loop else block ``` Runs if and only if the loop is ...
It is often used to code an empty body for a compound statement.
For instance, if you want to code an infinite loop that does nothing each time through, do it with a pass: ``` while True: pass # Type Ctrl-C to stop me! ``` Because the body is just an empty statement, Python gets stuck in this loop.
pass is roughly to statements as None is to objects—an explicit nothing.
Notice that here the ``` while loop’s body is on the same line as the header, after the colon; as with if state ``` ments, this only works if the body isn’t a compound statement. This example does nothing forever.
It probably isn’t the most useful Python program ever written (unless you want to warm up your laptop computer on a cold winter’s day!); frankly, though, I couldn’t think of a better pass example at this point in the book. We’ll see other places where pass makes more sense later—for instance, to ignore exceptions caug...
A pass is also sometime coded to mean “to be filled in later,” to stub out the bodies of functions temporarily: ``` def func1(): pass # Add real code here later def func2(): pass ``` We can’t leave the body empty without getting a syntax error, so we say pass instead. _Version skew note: Pyth...
(literally, three consecutive dots) to appear any place an expression can.
Because ellipses do nothing by themselves, this can serve as an alternative to the pass statement, especially for code to be filled in later—a sort of Python “TBD”: ``` def func1(): ...
# Alternative to pass def func2(): ... func1() # Does nothing if called ``` Ellipses can also appear on the same line as a statement header and may be used to initialize variable names if no specific type is required: ``` def func1(): ...
# Works on same line too def func2(): ... >>> X = ...
# Alternative to None ``` **|** **f** ----- ``` >>> X Ellipsis ``` This notation is new in Python 3.0 (and goes well beyond the original intent of ...