Test Your Knowledge: Answers

 

 
  1. 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.
  2. 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.
  3. The statements in a nested block are all indented the same number of tabs or spaces.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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!