Puzzle 14: Explanation
We’ll go over the concept of short-circuit operators in Python and why the code does not raise an exception.
We'll cover the following...
We'll cover the following...
Let’s try it!
Try running the code below to verify the result:
Press + to interact
Python 3.8
def div(a, b):return a / bif div(1, 2) > 0 or div(1, 0) > 0:print('OK')else:print('oopsie')
Explanation
Most people expect this code to raise ZeroDivisionError
due to div(1, 0)
.
If we call div(1, 0)
by itself, we’ll see that exception. Still, the logic ...