Andrew Breza

67%
Flag icon
The semantics of for/else, while/else, and try/else are closely related, but very different from if/else. Initially, the word else actually hindered my understanding of these features, but eventually I got used to it. Here are the rules: for The else block will run only if and when the for loop runs to completion (i.e., not if the for is aborted with a break). while The else block will run only if and when the while loop exits because the condition became falsy (i.e., not if the while is aborted with a break). try The else block will run only if no exception is raised in the try block.
Fluent Python: Clear, Concise, and Effective Programming
Rate this book
Clear rating
Open Preview