Practical Guide to Python Functions & Logic Practice
Transcript from the "Functions & Logic Practice" Lesson
>> We can create functions with the def keyword. So we had a function called add numbers. We can give our function optional arguments And, we want to, when we wanna call the function, we use the function name, and then we pass in the correct number of required arguments.
[00:00:30] If we pass in too many, or too few, you will see that this argument takes two positional arguments but three were given. Or this function is missing one required positional argument and it'll tell you which one. So in this case, the number of arguments matters. Remember that functions in Python don't have curly braces.
[00:00:57] So indentation is very important. If I tried to define a function here and I did not indent. This is a an indentation error. Python is telling me I expected an indented block to signify the start of the function. You did not give me one. So if you see an indentation error, it's likely that you're Indentation is not at the correct level.
[00:01:29] We talked about function scope. So if you have variables that are defined outside of your function. If you try to change them. If I check the value of x, it's two. If I call my function, it's three inside of the function, but that value of x that was defined outside of the function was not changed by this statement.
[00:02:04] Remember that we have positional arguments and keyword arguments. So if I have my add function here I can have an optional argument about what to do. Let's call this one operation. And There are two here, I need to get these names not values. All right, so I've defined my add_numbers function and I can say that if the operation is equal to the word add, we're going to return x + y.
[00:02:53] Otherwise, we're going to subtract and return x- y. So by default add_numbers will add. But if I pass in an operation that's something else, it will instead subtract. This keyword argument is optional. If I don't pass one in the default will be used. We talked a little bit about boolean logic.
[00:03:27] So comparisons in Python less than, less than equals, greater than, greater than equals, and then double equals and not equals. And a reminder that these operators test the objects value. You want to use is, and is not, in order to test if two objects are the same thing.
[00:03:51] And this is useful for those singletons that are defined in Python, like none and true and false. So, none, true and false are when you want to use the is keyword because you're not comparing two different values. You want to make sure that, the singleton is what with that variable is pointing out.
[00:04:21] We talked a little bit about truthiness. So instead of saying 3, or 5 less than 3 is equal to true. And seeing some weird unexpected behavior, because remember in Python under the hood, true is actually the number one. We want to just use five less than three, and that will give us the correct result.
[00:04:59] So here we had three less than five is equal to true as false, even though it should be true, right. So use this and not that. We talked about boolean operators. So x or y, x and y and not x. These work as expected, the same way that they do in other languages except in Python, we use the keywords or and a not.
[00:05:31] And that makes our statements read a little bit more like actual sentences in English, and makes Python code easy to understand. We talked about control statements, like if and else. So, typing if a true expression in this case, let's see, true, print(hi) otherwise, print Bye, this will print hi and not bye, if this was false we would instead skip all of the code inside of the if statement and jump straight to the code in the else statement.
[00:06:17] If we were to, we don't define any logic in the else statement. If we needed to define extra conditional statements, we would use the else if. So I could say if false print 1, elif true print 2 else, print 3, this condition is false. It's going to evaluate this one next, which is true.
[00:06:54] So it will go ahead and print 2. We can use these in complex ways by adding statements together. So, if we wanted to define fizzbuzz, for example For a number, we can say that if the number is divisible by three, and the number is divisible by five. Then print fizzbuzz.
[00:07:37] So fizzbuzz of three, I think I missed my let me double check this. I think I should have had parentheses around this. Let me know if any of you ran into the same issue in this exercise No, had it right the first time. Forgot what fizzbuzz did. So this only checks if it's divisible by both three and five, which 15 is.
[00:08:14] But three and five are not. So no parenthesis necessary.