Text
stringlengths
1
9.41k
This is by design; as a scripting language, one of Python’s goals is to make programmers’ lives easier by requiring less typing. More specifically, when you compare the two syntax models, you’ll notice that Python adds one new thing to the mix, and that three items that are present in the C-like language are not prese...
All Python com_pound statements (i.e., statements that have statements nested inside them) follow the_ same general pattern of a header line terminated in a colon, followed by a nested block of code usually indented underneath the header line, like this: ``` Header line: Nested statement block ``` The colon is r...
In fact, if you are new to Python, you’ll almost certainly forget the colon character very soon.
Most Python-friendly editors make this mistake easy to spot, and including it eventually becomes an unconscious habit (so much so that you may start typing colons in your C++ code, too, generating many entertaining error messages from your C++ compiler!). **|** ----- ###### What Python Removes Although Python requ...
In Python, though, they are not—we simply omit the parentheses, and the statement works the same way: ``` if x < y ``` Technically speaking, because every expression can be enclosed in parentheses, including them will not hurt in this Python code, and they are not treated as an error if present.
But don’t do that: you’ll be wearing out your keyboard needlessly, and broadcasting to the world that you’re an ex-C programmer still learning Python (I was once, too).
The Python way is to simply omit the parentheses in these kinds of statements altogether. ###### End of line is end of statement The second and more significant syntax component you won’t find in Python code is the semicolon.
You don’t need to terminate statements with semicolons in Python the way you do in C-like languages: ``` x = 1; ``` In Python, the general rule is that the end of a line automatically terminates the statement that appears on that line.
In other words, you can leave off the semicolons, and it works the same way: ``` x = 1 ``` There are some ways to work around this rule, as you’ll see in a moment.
But, in general, you write one statement per line for the vast majority of Python code, and no semicolon is required. Here, too, if you are pining for your C programming days (if such a state is possible...) you can continue to use semicolons at the end of each statement—the language lets you get away with them if the...
But don’t do that either (really!); again, doing so tells the world that you’re still a C programmer who hasn’t quite made the switch to Python coding.
The Pythonic style is to leave off the semicolons altogether. **f** **f** **|** ----- ###### End of indentation is end of block The third and final syntax component that Python removes, and the one that may seem the most unusual to soon-to-be-ex-C programmers (until they’ve used it for 10 minutes and realize it’s ...
You don’t need to include begin/end, then/endif, or braces around the nested block, as you do in Clike languages: ``` if (x > y) { x = 1; y = 2; } ``` Instead, in Python, we consistently indent all the statements in a given single nested block the same distance to the right, and Python uses the statements’...
Python doesn’t care how you indent (you may use either spaces or tabs), or how much you indent (you may use any number of spaces or tabs).
In fact, the indentation of one nested block can be totally different from that of another.
The syntax rule is only that for a given single nested block, all of its statements must be indented the same distance to the right.
If this is not the case, you will get a syntax error, and your code will not run until you repair its indentation to be consistent. ###### Why Indentation Syntax? The indentation rule may seem unusual at first glance to programmers accustomed to C-like languages, but it is a deliberate feature of Python, and it’s one...
It essentially means that you must line up your code vertically, in columns, according to its logical structure.
The net effect is to make your code more consistent and readable (unlike much of the code written in C-like languages). To put that more strongly, aligning your code according to its logical structure is a major part of making it readable, and thus reusable and maintainable, by yourself and others.
In fact, even if you never use Python after reading this book, you should get into the habit of aligning your code for readability in any block-structured language.
Python forces the issue by making this a part of its syntax, but it’s an important thing to do in any programming language, and it has a huge impact on the usefulness of your code. Your experience may vary, but when I was still doing development on a full-time basis, I was mostly paid to work on large old C++ programs...
Almost invariably, each programmer had his or her **|** ----- own style for indenting code.
For example, I’d often be asked to change a while loop coded in the C++ language that began like this: ``` while (x > 0) { ``` Before we even get into indentation, there are three or four ways that programmers can arrange these braces in a C-like language, and organizations often have political debates and write sta...
Ignoring that, here’s the scenario I often encountered in C++ code.
The first person who worked on the code indented the loop four spaces: ``` while (x > 0) { --------; --------; ``` That person eventually moved on to management, only to be replaced by someone who liked to indent further to the right: ``` while (x > 0) { --------; --------; --------; ...
Eventually, the block is terminated by a closing brace (}), which of course makes this “block-structured code” (he says, sarcastically).
In any block-structured language, Python or otherwise, if nested blocks are not indented consistently, they become very difficult for the reader to interpret, change, or reuse, because the code no longer visually reflects its logical meaning.
Readability matters, and indentation is a major component of readability. Here is another example that may have burned you in the past if you’ve done much programming in a C-like language.
Consider the following statement in C: ``` if (x) if (y) statement1; else statement2; ``` **f** **f** **|** ----- Which if does the else here go with?
Surprisingly, the else is paired with the nested ``` if statement (if (y)), even though it looks visually as though it is associated with the ``` outer if (x).
This is a classic pitfall in the C language, and it can lead to the reader completely misinterpreting the code and changing it incorrectly in ways that might not be uncovered until the Mars rover crashes into a giant rock! This cannot happen in Python—because indentation is significant, the way the code looks is the w...
Consider an equivalent Python statement: ``` if x: if y: statement1 else: statement2 ``` In this example, the if that the else lines up with vertically is the one it is associated with logically (the outer if x).
In a sense, Python is a WYSIWYG language—what you see is what you get because the way code looks is the way it runs, regardless of who coded it. If this still isn’t enough to underscore the benefits of Python’s syntax, here’s another anecdote.
Early in my career, I worked at a successful company that developed systems software in the C language, where consistent indentation is not required.
Even so, when we checked our code into source control at the end of the day, this company ran an automated script that analyzed the indentation used in the code.
If the script noticed that we’d indented our code inconsistently, we received an automated email about it the next morning—and so did our managers! The point is that even when a language doesn’t require it, good programmers know that consistent use of indentation has a huge impact on code readability and quality. The ...
In the IDLE Python GUI, for example, lines of code are automatically indented when you are typing a nested block; pressing the Backspace key backs up one level of indentation, and you can customize how far to the right IDLE indents statements in a nested block.
There is no universal standard on this: four spaces or one tab per level is common, but it’s up to you to decide how and how much you wish to indent.
Indent further to the right for further nested blocks, and less to close the prior block. As a rule of thumb, you probably shouldn’t mix tabs and spaces in the same block in Python, unless you do so consistently; use tabs or spaces in a given block, but not both (in fact, Python 3.0 now issues an error for inconsisten...
But you probably shouldn’t mix tabs or spaces in indentation in _any structured language—such code can cause major readability issues if the next pro-_ grammer has his or her editor set to display tabs differently than yours.
C-like languages **|** ----- might let coders get away with this, but they shouldn’t: the result can be a mangled mess. I can’t stress enough that regardless of which language you code in, you should be indenting consistently for readability.
In fact, if you weren’t taught to do this earlier in your career, your teachers did you a disservice.
Most programmers—especially those who must read others’ code—consider it a major asset that Python elevates this to the level of syntax.
Moreover, generating tabs instead of braces is no more difficult in practice for tools that must output Python code.
In general, if you do what you should be doing in a C-like language anyhow, but get rid of the braces, your code will satisfy Python’s syntax rules. ###### A Few Special Cases As mentioned previously, in Python’s syntax model: - The end of a line terminates the statement on that line (without semicolons). - Neste...
However, Python also provides some special-purpose rules that allow customization of both statements and nested statement blocks. ###### Statement rule special cases Although statements normally appear one per line, it is possible to squeeze more than one statement onto a single line in Python by separating them with...
This only works, though, if the statements thus combined are not themselves_ compound statements.
In other words, you can chain together only simple statements, like assignments, prints, and function calls.
Compound statements must still appear on lines of their own (otherwise, you could squeeze an entire program onto one line, which probably would not make you very popular among your coworkers!). The other special rule for statements is essentially the inverse: you can make a single statement span across multiple lines.
To make this work, you simply have to enclose part of your statement in a bracketed pair—parentheses (()), square brackets ([]), or curly braces ({}).
Any code enclosed in these constructs can cross multiple lines: your statement doesn’t end until Python reaches the line containing the closing part of the pair.
For instance, to continue a list literal: ``` mlist = [111, 222, 333] ``` **f** **f** **|** ----- Because the code is enclosed in a square brackets pair, Python simply drops down to the next line until it encounters the closing bracket.
The curly braces surrounding dictionaries (as well as set literals and dictionary and set comprehensions in 3.0) allow them to span lines this way too, and parentheses handle tuples, function calls, and expressions.
The indentation of the continuation lines does not matter, though common sense dictates that the lines should be aligned somehow for readability. Parentheses are the catchall device—because any expression can be wrapped up in them, simply inserting a left parenthesis allows you to drop down to the next line and contin...
Anywhere you need to code a large expression, simply wrap it in parentheses to continue it on the next line: ``` if (A == 1 and B == 2 and C == 3): print('spam' * 3) ``` An older rule also allows for continuation lines when the prior line ends in a backslash: ``` X = A + B + \ # An error-prone...
It’s also another throwback to the C language, where it is commonly used in “#define” macros; again, when in Pythonland, do as Pythonistas do, not as C programmers do. ###### Block rule special case As mentioned previously, statements in a nested block of code are normally associated by being indented the same amount...
As one special case here, the body of a compound statement can instead appear on the same line as the header in Python, after the colon: ``` if x > y: print(x) ``` This allows us to code single-line if statements, single-line loops, and so on.
Here again, though, this will work only if the body of the compound statement itself does not contain any compound statements.
That is, only simple statements—assignments, ``` prints, function calls, and the like—are allowed after the colon. Larger statements must ``` still appear on lines by themselves.
Extra parts of compound statements (such as the ``` else part of an if, which we’ll meet later) must also be on separate lines of their own. ``` The body can consist of multiple simple statements separated by semicolons, but this tends to be frowned upon. **|** ----- In general, even though it’s not always require...
Moreover, some code profiling and coverage tools may not be able to distinguish between multiple statements squeezed onto a single line or the header and body of a one-line compound statement.
It is almost always to your advantage to keep things simple in Python. To see a prime and common exception to one of these rules in action, however (the use of a single-line if statement to break out of a loop), let’s move on to the next section and write some real code. ###### A Quick Example: Interactive Loops We’...
To get started, let’s work through a brief, realistic example that demonstrates the way that statement syntax and statement nesting come together in practice, and introduces a few statements along the way. ###### A Simple Interactive Loop Suppose you’re asked to write a Python program that interacts with a user in a ...
Maybe you’re accepting inputs to send to a database, or reading numbers to be used in a calculation.
Regardless of the purpose, you need to code a loop that reads one or more inputs from a user typing on a keyboard, and prints back a result for each. In other words, you need to write a classic read/evaluate/print loop program. In Python, typical boilerplate code for such an interactive loop might look like this: ``` ...
We’ll study the while statement in more detail later, but in short, it consists of the word while, followed by an expression that is interpreted as a true or false result, followed by a nested block of code that is repeated while the test at the top is true (the word True here is considered always true). - The `input...
This would work either way, but as it’s coded, we’ve saved an extra line. - Finally, the Python break statement is used to exit the loop immediately—it simply jumps out of the loop statement altogether, and the program continues after the loop.
Without this exit statement, the while would loop forever, as its test is always true. In effect, this combination of statements essentially means “read a line from the user and print it in uppercase until the user enters the word ‘stop.’” There are other ways to code such a loop, but the form used here is very common...
Either the end of the source file or a lesser-indented statement will terminate the loop body block. When run, here is the sort of interaction we get from this code: ``` Enter text:spam SPAM Enter text:42 42 Enter text:stop ``` _Version skew note: This example is coded for Python 3.0.
If you are_ working in Python 2.6 or earlier, the code works the same, but you should use raw_input instead of input, and you can omit the outer parentheses in print statements.
In 3.0 the former was renamed, and the latter is a built-in function instead of a statement (more on prints in the next chapter). ###### Doing Math on User Inputs Our script works, but now suppose that instead of converting a text string to uppercase, we want to do some math with numeric input—squaring it, for exampl...
We might try statements like these to achieve the desired effect: ``` >>> reply = '20' >>> reply ** 2 ...error text omitted... TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' ``` This won’t quite work in our script, though, because (as discussed in the prior part of the book) Python won...
We cannot raise a string of digits to a power unless we convert it manually to an integer: **|** ----- ``` >>> int(reply) ** 2 400 ``` Armed with this information, we can now recode our loop to perform the necessary math.
Type the following in a file to test it: ``` while True: reply = input('Enter text:') if reply == 'stop': break print(int(reply) ** 2) print('Bye') ``` This script uses a single-line if statement to exit on “stop” as before, but it also converts inputs to perform the required math.
This version also adds an exit message at the bottom.
Because the print statement in the last line is not indented as much as the nested block of code, it is not considered part of the loop body and will run only once, after the loop is exited: ``` Enter text:2 4 Enter text:40 1600 Enter text:stop Bye ``` One note here: I’m assuming that this code is stored i...
If you are entering this code interactively, be sure to include a blank line (i.e., press Enter twice) before the final print statement, to terminate the loop.
The final print doesn’t quite make sense in interactive mode, though (you’ll have to code it after interacting with the loop!). ###### Handling Errors by Testing Inputs So far so good, but notice what happens when the input is invalid: ``` Enter text:xxx ...error text omitted... ValueError: invalid literal for ...
If we want our script to be robust, we can check the string’s content ahead of time with the string object’s isdigit method: ``` >>> S = '123' >>> T = 'xxx' >>> S.isdigit(), T.isdigit() (True, False) ``` This also gives us an excuse to further nest the statements in our example.
The following new version of our interactive script uses a full-blown if statement to work around the exception on errors: ``` while True: reply = input('Enter text:') ``` **|** ----- ``` if reply == 'stop': break elif not reply.isdigit(): print('Bad!' * 8) else: print(int(reply)...
In its full form, it consists of the word if followed by a test and an associated block of code, one or more optional elif (“else if”) tests and code blocks, and an optional else part, with an associated block of code at the bottom to serve as a default.
Python runs the block of code associated with the first test that is true, working from top to bottom, or the else part if all tests are false. The if, elif, and else parts in the preceding example are associated as part of the same statement because they all line up vertically (i.e., share the same level of indentati...
In turn, the entire if block is part of the while loop because all of it is indented under the loop’s header line.
Statement nesting is natural once you get the hang of it. When we run our new script, its code catches errors before they occur and prints an (arguably silly) error message to demonstrate: ``` Enter text:5 25 Enter text:xyz Bad!Bad!Bad!Bad!Bad!Bad!Bad!Bad! Enter text:10 100 Enter text:stop ###### Handli...
We’ll explore this statement in depth in Part VII of this book, but as a preview, using a try here can lead to code that some would claim is simpler than the prior version: ``` while True: reply = input('Enter text:') if reply == 'stop': break try: num = int(reply) except: print('Bad!' * 8...
This try statement is composed of the word try, followed by the main block of code (the action we are trying to run), followed by an ``` except part that gives the exception handler code and an else part to be run if no ``` exception is raised in the try part.
Python first runs the try part, then runs either the ``` except part (if an exception occurs) or the else part (if no exception occurs). ``` In terms of statement nesting, because the words try, except, and else are all indented to the same level, they are all considered part of the same single try statement.
Notice that the else part is associated with the try here, not the if.
As we’ve seen, else can appear in if statements in Python, but it can also appear in try statements and loops— its indentation tells you what statement it is a part of.
In this case, the try statement spans from the word try through the code indented under the word else, because the ``` else is indented to the same level as try.
The if statement in this code is a one-liner ``` and ends after the break. Again, we’ll come back to the try statement later in this book.
For now, be aware that because try can be used to intercept any error, it reduces the amount of error-checking code you have to write, and it’s a very general approach to dealing with unusual cases. If we wanted to support input of floating-point numbers instead of just integers, for example, using try would be much ea...
Nesting can take us even further if we need it to—we could, for example, branch to one of a set of alternatives based on the relative magnitude of a valid input: ``` while True: reply = input('Enter text:') if reply == 'stop': break elif not reply.isdigit(): print('Bad!' * 8) else: n...
When code is conditional, or repeated like this, we simply indent it further to the right.
The net effect is like that of the prior versions, but we’ll now print “low” for numbers less than 20: ``` Enter text:19 low Enter text:20 400 Enter text:spam Bad!Bad!Bad!Bad!Bad!Bad!Bad!Bad! Enter text:stop Bye ###### Chapter Summary ``` That concludes our quick look at Python statement syntax.
This chapter introduced the general rules for coding statements and blocks of code.
As you’ve learned, in Python we normally code one statement per line and indent all the statements in a nested block the same amount (indentation is part of Python’s syntax).
However, we also looked at a few exceptions to these rules, including continuation lines and single-line tests and loops.
Finally, we put these ideas to work in an interactive script that demonstrated a handful of statements and showed statement syntax in action. In the next chapter, we’ll start to dig deeper by going over each of Python’s basic procedural statements in depth.
As you’ll see, though, all statements follow the same general rules introduced here. ###### Test Your Knowledge: Quiz 1. What three things are required in a C-like language but omitted in Python? 2.