[MUSIC] In this video I'm going to talk about assertions. Assertions are a way of checking that certain conditions hold within your program. All right? And Python has an assert statement that allows you to do this. All right? And when you use assert, it basically checks the condition and if the condition is false, it immediately throws an exception and stops your program. All right? This allows you to immediately understand, hey, there's something wrong and go look into it. All right. Now when you're, writing and debugging a program, okay, it's often very useful to find out about the bugs, as soon as they occur in the program. You don't want to let bad values propagate through the program and cause unintended effects later. You want to catch them as soon as possible and, assertions are one way of doing this. So let's take a look at how they work. All right, I'm going to use some very simple examples here to demonstrate the use of assertions in Python. And I want to make something clear, okay? You would probably not use assertions in these functions here, all right? The, mainly the way you want to use assertions is if you have a complicated piece of code where it's relatively easy to check if the result of that is valid, okay. So I know how to check, you know, here's the set of valid possibilities. I know how to check that you're in those possibilities, even if I don't know what the correct answer is, all right. And that at least gives you a sanity check that you didn't compute something this is completely invalid, all right. And usually in these cases, it's easier to check if something is valid than it is to look and verify the correctness of the code. Okay? So this is a good sanity check. Here, this code is going to be simple enough that you're probably going to be able to look at it and know, hey, that code is wrong, I know how to fix the code immediately. All right? So bear with me here. The idea here is to understand how I might use assertions, not to say these functions require assertions. Okay? All right so first, I want to write a function here that calculates the length of a hypotenuse of a right triangle. So I take the length of the two sides, and I'm going to return the length of the hypotenuse, okay? Now, I know some things about right triangles. Okay, so I'm going to add some assertions here. First thing I know is, well, if I have a right triangle then the length of the hypotenuse has to be larger than the length of either side. Okay? If I don't get that then I know that I screwed something up. Okay? I also know something about triangles and I know that the length of the hypotenuse has to be less, than the sum of the other two sides. If it's not, then I can't even make a triangle here. Okay? So I know these two things. I'm going to put two insertions that are checking, that these things are true. Let's see how my hypotenuse function works out here. Let's print. 'Kay, so I'm trying to take the hypotenuse of a triangle that has sides three and four. Hm. First thing I find out is the hypotenuse is too short. Oh, what's going on here? My friend told me how to do this. You know Joe, you know, gave me a little tip here. That the hypotenuse is the square root of something or other. So I assumed it's just a sum of you know, the sides here, but that's not right. So wait, oh I think he said something about raising something to a power. Right, let's cube them both. All right, square root of the cubes. That must be right, right? Hm, no, that gives me a, oh, now I remember what he said, all right. It's the square root of the sum of the squares. That's how I calculate the hypotenuse, right? Okay five, yes, I know the three, four, five right triangle, okay? But hopefully here, by doing this, you saw what happened with the assertions, right? When I have something that's incorrect, these assertions fail. So let's go back to, you know, the cubes, sum of the cubes. Okay, what happened here, my hypotenuse was too long. So this assertion here that says hype less than side a plus side b, okay that was false because my hypotenuse is too big, all right, and so I get this message here, hypotenuse too long. So here's how the assert statement works. You have assert. Then you have some condition. 'Kay? That condition gets evaluated. If it's true, great. You're asserting then it must, it's supposed to be true. It's true, the, the program continues on. If it's false, all right, it throws an assertion error. And the message you get is whatever you put over here. And you can build as sophisticated a string as you want to prevent, print, print as much information as you want. Okay? Now when I fix this and everything works just fine. Okay? I passed the two assertions because, the hypotenuse is valid. It's larger than the length of either side and it's less than the sum of the lengths of the sides. Now that does not mean that I calculated the right thing, those are just quick sanity checks that make sure that I didn't calculate something that's wildly wrong. Now I do know there's the three, four, five right triangles so I do know that I now have the right answer. Right, again, I could figure that out by inspection. I could have looked at my program to understand that it was [LAUGH] clearly incorrect, right? Let's look at another example here, all right? This function called odd fraction. All right, what I'm doing is taking a list of numbers and I want to return the fraction of the numbers in that list that are odd. Okay, and all right, I'm going to divide by total down here at the bottom. All right, I have fraction is the float of the number of odds divided by the total, you know, number that I had. If total was zero, I'm going to get a divide by zero error. Well, okay so let's, before we do that, let's just assert that total isn't zero, because it doesn't make any sense, if it is. All right, I screwed up. So, let's print. I've got my list. Print the odd fraction here. I got an assertion error, where there's no elements, but clearly there are elements. I passed in this list that has, like seven elements in it. So, let's look at the code more carefully. oh, yeah, I screwed up. I never actually incremented the count here. If I add a line total plus equals 1. All right, now I have a, a total that is not zero so it at least passes that sanity check and maybe I want to add another one here. To say that, assert odds is less than total, right? That makes sense. Too many odds, if that's not true. Okay, let's run it again, make sure, okay, I passed that assertion too. All right, and the final fraction is 0.57 here, in that list. Okay, because I have, you know, one, two, three, four odds out of seven. Okay? All right, hopefully that gives you a feel for how I might use assertions in my code. And again, I want to point out that these functions are probably way too simplistic or easy, you know, for you to sort of just look at the code and try to understand what's wrong than to use these kinds of assertions. As you write more complicated programs, assertions can be a very powerful tool to help you catch errors right as they occur. And the Python assert statement is very easy to use, it just takes a condition and a message and it evaluates the condition, if it's true, all is well and the program continues as expected. If it's false, it throws an assertion error and prints out the message for you, allowing you to catch the error right as it occurs, okay? So, if you think carefully about your program and what conditions should hold at what points, you can use assertions to help make the process of development and debugging much easier. All right? Does this mean you should start putting assertions everywhere in your code? No. Okay, don't overboard here, all right? You can spend more time, putting assertions in that don't really help you that much than you do actually programming. You want to think about the points where, here's a complex calculation, let's make sure that I got the right result before I start moving on. Or at least, let's make sure I didn't get a wildly wrong result before I start moving on, okay? And if you think carefully and use assertions wisely. It can be very helpful in you, for you as you develop your programs.