Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Zero Division

00:00 Here, we’re going to see how the Python or operator can present one solution to writing arithmetic programs that might potentially divide by zero.

00:11 In mathematics, division by zero is undefined and it will cause a Python program to crash if you attempt it. So if I try to do something meaningful like 5 / 3, Python will tell me what that is.

00:27 But if I try to take 5 / 0, I get a division by zero error. And if you have a division calculation in your program where the denominator could be a variable, you might want to take steps to prevent the program from crashing.

00:43 There are lots of solutions to this. The one we’re going to look at today involves using the word or. So, one solution is to create your own division function.

00:58 It’s going to perform a divided by b and check if b is not 0,

01:07 then we can return a / b. And so I can do divide(), 5 divided by 3. I get the same result as before.

01:24 And if I try to divide 5 by 0, the program doesn’t crash. The function doesn’t do anything. How can we communicate that division by zero has occurred and allow other code to be written around that?

01:42 Well, one solution would be to take advantage not just of the or operator, but of short-circuit evaluation, where I’ll check to see if b is 0 and if it evaluates to True, I won’t attempt to do the division.

01:58 This operation will stop and I’ll return True, which can be used by the part of the program that just attempted to do the division to see if a meaningful value was obtained or not.

02:11 If b isn’t 0, this evaluates to False and I do the second part of the or operation, which is to do the division. And I know when I get here that b will not be 0, because if it was, we would have returned from this operation by this being True. So again, a subtle difference. I’m going to define a new divide(),

02:45 which is simply going to return True if b is 0. If b is not 0, then the second part of this or expression will be evaluated, which is a / b.

03:01 So now if I try a meaningful division, b is not 0, so we do the division a / b. It gives us the value that we want.

03:14 On the other hand, if I do attempt division by zero, it returns True. Importantly, it doesn’t crash, and also I get a value back that I can then use to determine if I had a valid division calculation done or not.

03:35 So now that we’ve defined this divide() function, let’s see how it might be used. I have here a few code snippets. So first, we have our divide() function, which will return True if we’re trying to divide by zero. And if we’re not, it will actually return the quotient of a divided by b.

03:57 And here’s a function where we might use that, called dividing() for lack of a better term. We prompt the user for values for a and for b and then we call the divide() function, but being sure to save the result to a variable before we try to use that.

04:15 We know that if this method returns True, we tried to do division by zero, so the program should act accordingly. In this case, we’re just going to print "no result, division by zero attempted".

04:30 If quotient is not identically equal to True, then it returned a number and we can output that result or use that however we need it.

04:42 So if we want to see this in action,

04:47 we will import this file,

04:52 I call dividing(). It asks for the first number, say 9. It asks for the second number, divide 9 by 2, and we get 4.5. If I call dividing() again and this time we try to divide by zero, we get that warning.

05:15 What you do in an actual program would probably be more meaningful than this. This is just to illustrate how we can check the divide() function’s result before actually trying to use it.

05:29 We have one more example to look at, and that involves a lambda calculation, where or can let us do some pretty cool things.

Become a Member to join the conversation.