Check out a free preview of the full Python Fundamentals course
The "Practice: Functions" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:
Nina live codes the solution to the exercise.
Transcript from the "Practice: Functions" Lesson
[00:00:05]
>> Nina Zakharenko: Talking about functions, we're gonna create a basic function, add_numbers. It's gonna take two arguments, x and y, and we're going to return x + y, right? So we can call add_numbers on any integers, or floats, or whatever. If we also made some variables, we can call add_numbers with the variables, right, and still get the right result.
[00:00:43]
>> Nina Zakharenko: Now, remember the importance of whitespace. If you try to make a function, and you don't do the level of indentation, you're gonna get an indentation error, right? Gonna say, expected an indented block, you didn't provide one. That's one of the things I really love about Python. As the language has grown, the error messages have gotten a lot simpler, and more descriptive, right?
[00:01:16]
Now that you know the terminology for these things, the errors are pretty easy to read, right? It expected an indented block, and you didn't provide one.
>> Nina Zakharenko: Okay, so a little bit about function scope, and then I'm gonna show you something else that was not part of this exercise.
[00:01:35]
So I declared x = 1, y = 2,
>> Nina Zakharenko: I have an add_numbers method here,
>> Nina Zakharenko: I'm gonna say, an f-string, inside the function, x is x and y is y,
>> Nina Zakharenko: And I'm gonna return x+y.
>> Nina Zakharenko: Okay, so printing out outside of the function, x is x, y is y, what was the result here?
[00:02:24]
>> Nina Zakharenko: What will I see for x and y?
>> class: 1 and 2.
>> Nina Zakharenko: 1 and 2, whoops,
>> Nina Zakharenko: I have an invalid syntax. There we go, thank you, it was pointing right at the right thing, right, okay, outside of the function x is 1, y is 2. If I call add_numbers(4, 5), what is gonna be printed out?
[00:02:56]
>> class: 4 and 5.
>> Nina Zakharenko: Right, inside the function, x is 4, y is 5.
>> Nina Zakharenko: Let's try something else, so we have an x declared, and we have a y declared, right?
>> Nina Zakharenko: Let's say I make a new function, numbers(), doesn't take any arguments, and I set x = 0 and y = -1.
[00:03:24]
And I'm gonna print out the same thing, right, f-string, inside the function,
>> Nina Zakharenko: x is x, y is y, okay? If I call numbers(), inside the function, x is 0, y is 1.
>> Nina Zakharenko: I'm gonna print out, again, outside of the function, x is x and y is y.
[00:03:58]
So these are old values that we declared, right, x is 1, y is 2. So you can change the value of x and y within the scope of your function, that doesn't affect the outer scope, makes sense, right? Okay, positional arguments versus keyword arguments.
>> Nina Zakharenko: So let's make a new function, we're gonna say calculate_numbers, and it's gonna take an x and y in an operation,
[00:04:34]
>> Nina Zakharenko: ="add", right, and we'll talk about if-statements soon, but in the meantime, pretty easy syntax to follow. So we're just gonna say, if the operation is add, we're gonna return x + y. Something to note here is, if-statements have their own level of indentation. So I indent once to get to the indentation for the function, I indent again to get to the indentation level of the if-statement.
[00:05:01]
So here I'm gonna return x + y, and I'm just gonna have another if=statement here. And if the operation is subtract, i'm gonna return x- y.
>> Nina Zakharenko: Okay, so calculate_numbers, calling it on 4 and 5, what will the result be?
>> class: 9.
>> Nina Zakharenko: 9, okay, what if I pass in 4 and 5, and I say that the operation is subtract, what will the result be?
[00:05:44]
>> class: -1.
>> Nina Zakharenko: -1-
>> Student: Nothing, because you misspelled subtract in the function.
>> class: No, thank you.
>> Student: You're welcome.
>> class: I love group pair programming. So I'm just gonna call this substract, to match my typo, just so we can kinda get on with it, okay, so -1. What if I call calculate_numbers with y = 3 and x = 2?
[00:06:18]
>> Student2: It'll add them, 5.
>> Nina Zakharenko: 5, that's right, awesome, you guys are Python pros now, that's it, we can just leave for the day.
>> class: [LAUGH]
>> Nina Zakharenko: Does this make sense to everyone, right, yeah, passing in 4 and 5, positional arguments, in order, required. If I try to say calculate_arguments with no arguments, missing 2 required positional arguments, right?
[00:06:42]
If I make labels, of if I pass in labels for the arguments, along with my positional arguments, just, I can put them in any order I want, right? I'm giving Python a hint about what I mean.
>> Nina Zakharenko: Okay, any questions about functions, yes?
>> Student3: In your conditional, you are using ==, but when we did whether or not the list was None, you said is, is there a difference between?
[00:07:13]
>> Nina Zakharenko: Yeah, that's a very good question, I'm gonna cover it in more detail later. But for the time being, there are a few types of built in-types in Python where you didn't have to declare it or anything, it's just there in your environment. None is one of them, True is one, False is another, those are Boolean statements.
[00:07:39]
And I'd have to double-check this, but I'm 95% positive, these values are singletons in Python, so they're only declared once. The difference between ==, == stands for equality, are these two things equal to each other? Whereas is stands for, do these things point to the same thing in memory?
[00:08:13]
>> Nina Zakharenko: So in general, you'll use the is keyword to compare to None, True, and False, kind of these built-in types that exist as part of the language. But you'll use equality to compare values, to compare if values are equal, but good eye.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops