已读28%
预计阅读本页时间:-
预计阅读本页时间:-
Test Your Knowledge: Answers
- C-like languages require parentheses around the tests in some statements, semicolons at the end of each statement, and braces around a nested block of code.
- The end of a line terminates the statement that appears on that line. Alternatively, if more than one statement appears on the same line, they can be terminated with semicolons; similarly, if a statement spans many lines, you must terminate it by closing a bracketed syntactic pair.
- The statements in a nested block are all indented the same number of tabs or spaces.
- A statement can be made to span many lines by enclosing part of it in parentheses, square brackets, or curly braces; the statement ends when Python sees a line that contains the closing part of the pair.
- The body of a compound statement can be moved to the header line after the colon, but only if the body consists of only noncompound statements.
- Only when you need to squeeze more than one statement onto a single line of code. Even then, this only works if all the statements are noncompound, and it’s discouraged because it can lead to code that is difficult to read.
- The try statement is used to catch and recover from exceptions (errors) in a Python script. It’s usually an alternative to manually checking for errors in your code.
- Forgetting to type the colon character at the end of the header line in a compound statement is the most common beginner’s mistake. If you haven’t made it yet, you probably will soon!