[1]: https://docs.python.org/3.6/reference/compound_stmts.html#wh...
[1]: https://docs.python.org/3.6/reference/compound_stmts.html#wh...
The 'try' block contains code which might raise an exception.
It can be followed by zero or more 'except' blocks, each of which indicates the type(s) of exception it will catch and contains code to execute in the event of that type of exception.
The 'except' block(s) can be followed by one 'else' block, which contains code to execute in the event no exception was raised by the code in the 'try' block.
And then there is optionally a 'finally' block, containing code that must be executed in all circumstances.
while x < 2:
...
else:
print('>= 2')
is the same as while x < 2:
...
print('>= 2')
unless you use a break. Just seems unnecessarily confusing to me. I have used for ... else though.Quoting documentation: "A break statement executed in the first suite terminates the loop without executing the else clause’s suite."
But indeed, in my experience, while+else are much less common that for+else. I'm not sure why.