Names in the target list are not deleted when the loop is finished, but if the sequence is empty, they will not have been assigned to at all by the loop. Hint: the built-in function range() returns an iterator of integers suitable to emulate the effect of Pascal’s$_{for i := a to b do}$ ; e.g.,$_{list(range(3))}$ returns the list$_{[0, 1, 2]}$ . 

Note: There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, e.g. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g., 

for x in a[:]: if x <0: a.remove(x) 

## 8.4 The $_{try}$statement 

The$_{try}$ statement specifies exception handlers and/or cleanup code for a group of statements: 

try_stmt ::= try1_stmt | try2_stmt $_{try1_stmt}$::= "try" ":" suite ("except" [ expression ["as" identifier ]] ":" suite )+ ["else" ":" suite ] ["finally" ":" suite ] $_{try2_stmt}$::= "try" ":" suite "finally" ":" suite 

The$_{except}$clause(s) specify one or more exception handlers. When no exception occurs in the$_{try}$ clause, no exception handler is executed. When an exception occurs in the$_{try}$suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression-less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with an exception if it is the class or a base class of the exception object, or a tuple containing an item that is the class or a base class of the exception object. 

If no except clause matches the exception, the search for an exception handler continues in the surrounding code and on the invocation stack. 1 

If the evaluation of an expression in the header of an except clause raises an exception, the original search for a handler is canceled and a search starts for the new exception in the surrounding code and on the call stack (it is treated as if the entire$_{try}$ statement raised the exception). 

When a matching except clause is found, the exception is assigned to the target specified after the$_{as}$ keyword in that except clause, if present, and the except clause’s suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.) 

When an exception has been assigned using$_{as target}$ , it is cleared at the end of the except clause. This is as if 

except E as N: foo 

was translated to 

