 Hello everyone. So in this video I want to introduce you or remind you of two important concepts in Python that we will use as we go about implementing our more sophisticated data structures to get them to operate a little more seamlessly and more in line with how Python does its own processing. So this is a short video to kind of introduce you to the concept of exception handling and default parameters. Right? So I know some folks have heard of exception handling and you all have certainly seen these things, right? So suppose you have a, this is just a regular old Python list with Alice, Bob and Charlie in it, and then we call names dot remove Dave, right? So what happens? Go ahead and run this code if you so choose and what you will see is that you will get an error that looks like this, right? Names dot remove Dave. And then this is the offending line of code. It's selling me line two of my line of code. Value error. List dot remove X. X is not in the list. Okay? So it's saying, hey, you called remove with Dave, but Dave isn't here, right? So it's giving a value error. And that is by design, right? Python is saying, it's going to tell you you did something unexpected. You did something exceptional. And I don't know what to do, right? That is what an exception means. It's basically Python's way of saying, you tried to do something I don't know how to do. Here's an exception. I'm going to make your screen turn red. Fix your stuff or you deal with it, not me. You deal with it. I don't know what you want to have happen, right? Maybe you, the programmer, if Dave isn't in this list, maybe you don't care. And you just kind of move on with your life and like, Oh, okay. Oh, great. He's not there. Fine. I move on and I do more work. But by default, if an exception is raised, that's what we call it. This is an exception has been raised. The code stops, right? So this line of code right here, this print line will not execute, oops, excuse me, right? This print line will not execute. Maybe I can get the laser pointer. There we go. When an exception is raised, code program stops, you know this all too well, right? So what are exceptions really? There's a difference between exceptions and syntax errors. So syntax errors are when your Python code is malformed. They are detected before the code runs. You got indentation problems, you got parentheses problems, you forgot a colon. That's a syntax error and Python will yell at you for that. Exceptions are errors that are detected during execution. In other words, while the code is running. Exceptions are due to errors in logic, programming logic, and they cause an error in Python. So divide by zero will raise an exception. Trying to access a list element that is out of range. So you have a three element list and you say, give me list index 10. You get an exception. Trying to add two items but say one is a string and one is an integer. You will get an exception. You've probably seen some of these things before. Those are exceptions. Exceptions are the error mechanism in Python. So you can handle exceptions. And that is the formal term. Exception handling. Go to your book, read chapter 1.11 about this. So if we take our list code from before, Alice Bob and Charlie are in names. And then we try to remove a particular person. Specify a person to remove and then we ask the user to type in something. It will say we can handle an exception using this form. We use the key word try followed by a colon. And then the line of code we think might produce an exception. It might produce it depending on what the user inputs here. We don't know. But if an exception occurs, we say accept and then the type of exception you want to handle. So if this thing throws or raises, it raises the Python term. If this line of code raises a value error, this says catch it. Grab it. Grab it. This is a key word accept and then the type. And then instead of crashing out the program, which is what would normally happen, just print an error that says I couldn't find that person and move on with your life. The rest of the code will now execute. Because you have handled this exception, this line handles the exception, but it must be paired with this. You try some code, try to execute this code, and accept, if it has an error, catch it and do something. Handle it and do something. This whole thing together is called a try accept block. It kind of looks like an if-else statement. But instead of if-else error, we do try accept. So you can handle exceptions if desired. Often when you get to writing like real code, you will do this. The other thing that you can do, and that we are going to do in your next programming assignment, is that you can raise the exceptions. You can create the exceptions. Python lets you do that, which can be very handy when you're writing code and your code encounters a scenario that it doesn't know what to do. It doesn't know how to handle it. You can raise an exception and say, I don't know what to do here. You, the programmer who's using my code, you deal with it. Here's the error. You deal with it. So you, as we make our data types and make our data structures, you may want to raise exceptions to indicate that your code has encountered something it doesn't know how to handle. So here's how you can do that manually. I've got a very simple function defined here. It's called divide. It takes a numerator and a denominator. And as you know, you cannot divide by zero. That is undefined. So I have introduced a check in this function that says, if the denominator is equal to zero, raise a value error. So raise is the keyword. And it says, okay, here's an error. I don't know how to deal with this. Raise it. And then, unless the programmer who's using this code, which could be you, catches, tries and accepts that error, handles that exception, you're going to get red stuff all over your screen. It just won't work. Actually, you know what? Why don't I flop over here to PyCharm real quick. And I'll just show you. Let me make a new scratch file real quick. Scratch, Python. Let me make this a little big so you don't see it. Whoops. All right. There we go. All right. So I defined a, I was defining a function called divide. And the first thing I did was I checked to see if denominator equal equal zero. I raise a value error. Okay. And then I say, if a value error occurs, this line won't run. Okay. If a value error doesn't occur, though, then go ahead and do the division, right? So now if I call this function, this very simple, silly function, right? You don't really need this function, right? But if I call it in the denominator is zero, I should get a value error. So let me run this. And there it is, right? Raise value error. And it just says value error here. But it killed the program, right? Hi. I didn't make it. It killed the program right here. You raised the exception. The exception is uncaught. You can make these things more descriptive. This is actually a constructor. You know what? I'm not going to get into that. I'll give you examples of that later. But if you give it something that does work, divide 15 by 5, for example, everything runs, right? And I get my print statements. Okay. You can also handle this exception right here. Try, um, divide 10 by 0. And I accept. And if I happen to get an error right here, I can say, I can handle it. I can do something useful. Hey, you can't divide by 0. I ran it. You can see here. Try I divided by 0. I did get this value error. I handled it though. And instead of crashing out my program, I say, hey, you can't divide by 0. And then I print, you know, I continue processing. So this line actually goes through. All right. So exception handling. We're going to get some experience with that. But I just wanted to point you to it. So please have a look at your book in that regard. All right. So one final thing I want to show you here is the concept of default parameters. Okay. So I've got a very simple function defined here called say name. And it says print, hi, my name is first, last, using nef string. And then I run the code. Well, if you try and run this code, what you get is an error. And it will say type error. You've probably seen this. Say name is missing two required positional arguments, first and last. Well, that's because I called say name and I didn't give it a first and a last. And that's, you know, this error is to be expected. This error is kind of a good thing because it's saying, or this exception is a good thing because it's saying you tried to use this code, but you didn't use it the right way. So fix it. Use it the right way, darn it. Now, if you want to have some flexibility though in how your code behaves, what we could do here is give say name some default parameters so that if, let's say the user only gives a first name, it's happy with that. Let me show you what I mean. Okay, so up here I've got my same function except for each parameter, I am giving it a default value. So I do that by saying the parameter name first equals getsSammy, right? Sammy the string. And last, its default value equals seahawk. So what will happen now is that if we run this, if I call it and I give it no parameters well, what happens? First becomes Sammy and last becomes seahawk. And so this will say, hi my name is Sammy Seahawk. If I call say name and I only give it one parameter, it will substitute these arguments in the order that they are received. So Samantha here becomes the first parameter, but I didn't give a last a value for last. So the default value of seahawk is used for last. So what gets printed out the second one is, hi my name is Samantha, who goes in the first, Seahawk. But then of course I can provide it with both of the parameters. My name is say name Samantha White. Well, I don't use either of these default values. Hi my name is Samantha White. So this lets you specify default behavior for your functions. You can use this default behavior to make your functions a little more flexible and also maybe to avoid error scenarios. But certainly you don't need to use default parameter values except in certain scenarios. And when we get into coding the link list, I will show you one of the scenarios that you do use something like that for. Okay, so that's all for this video. Go back check out the slides, try these things out, get comfortable with them because we will be using them. And the good news is this basically represents the end of new Python stuff. And many of you have probably already seen these things before. Now we're really going to use them for their intended purpose. But yeah, good news this is really basically the very last new Python stuff that I will talk about in this class. Everything else is going to be used in Python syntax and Python tools you've seen before. Alright Okay, see you in the next video where we get into actually implementing some of the linked list operations.