Practice Exams:

Python Institute PCAP – Functions and Variable Scope Part 2

  1. Scope and Nested Functions

And we’re going to explore further the concept of scope and variable scope and how that changes. So you’ve seen how to define methods before, right? We’ve got this increase age method and then you see that there’s this nesting going on here. Four spaces, or a tab basically means that whatever is inside of the body of this method needs to be four spaces in. So I can do stuff in this method, you know, I could do all types of instructions, write comments and so on. And this will all be the body of this particular method, right? You’ve seen that. And outside of this, this is outside of this function’s scope, all of these other lines of code. So let’s go further into this method, or excuse me, this function, I keep calling a function a method and a method of function. The distinction is that a method is basically a function that’s associated with a class, whereas just a function is not associated with a class. You’re going to get into what that distinction means in more detail later, but understand that that’s why I sometimes mix them up. A lot of people mix those terms up as well, so don’t get confused by that. Understand that this is just a function that’s defined in a file and that’s why it should be called a function. So now let’s define a function within a function.

So inside of this function I can have another function and it’s of course going to be in a nestation inside of here, right? And so we need the keyword def for defining a function. And I’ll try to add the number four to the given age. So we’ll say add four to age. All right? This is the nested function inside of this already defined function. And so in the body of this function, it’s going to of course be four spaces in, right? So as soon as you press Enter, after this colon, the editor is smart enough to know that we’re now in the body of this function. So this is the concept of nest, nestation. So now I’m going to define the body of this function. And so we’ll say that age is equal to age plus four. Okay? Right off the bat it’s going to give an error. Let’s hover our mouse here on this red underline of the age. And it’s saying unresolved reference age. It doesn’t know what this is. What age are we talking about here? So it does not see age here, right? This function, as far as it’s concerned, this variable age is not defined anywhere. So it would need its own age defined up here.

So that’s not going to work. But if we pass in this particular age value to this function as a parameter and when we run this, you’re going to see that this value gets passed in. So now the editor is not complaining. It knows what age is in this given scope. So if I print let’s print nested method. And I’m just going to print the value of age. Now, of course, since we can’t concatenate strings with integers like this, we need to use the STR function that we’ve already seen before, all right? And so now let me change this to increase age. If I run this, notice that nothing is really going to happen. All right? This printing is not going to happen. Can you guess why? Take a guess as to why this printing will not happen and I’ll explain to you when I run this. So let’s run it. Notice it’s still printed 27. The previous two print line statements that we have here, why didn’t it print this? Well, we’ve defined this function, but we haven’t actually run it. We haven’t run this function.

We just define it inside of this. When we run this, it runs everything that’s in the body of this function, which is all of this code. But as part of this code, it’s not running this function, it’s just defining it. So let’s actually run this function as part of this outer function. So the way we do that is outside of this nestation. This is the definition of the inner function. Outside of this, we actually call the function called add four to age. Like that and we can pass in some value. So if I pass in the value 22 and run this, notice it says nested method 26. So it added the number four to the age that was sent, which was 22 to this. Now, if I just choose age here, what age is being passed to this function, it’s of course, going to be the age that’s defined in the body, in the outer body of this function. So this age is being sent to this add four to age. So let’s run this and notice now it’s 34. Now, even though we are assigning age an increment of four, in the body of this method, age is 34.

But outside of the body of that method, age is still 30, right? Because the scope of the outer method, its age variable contains the value 30. So if I was to, for example, print age, what do you think is going to print on this line? Let’s run it and notice it’s going to show 30, right? Because we are now outside of the scope of this. And we’re in the scope. We’re in the scope of this. Okay? And as far as this function scope is concerned, age is 30. It does not change the value of the age variable. Only internally did it change it when you pass it as an argument. But that does not affect the global age, which is up here. So hopefully you’re beginning to see how variable scope works. We’ve got the global scope, which is age right here. This is global to this entire script or this entire file. And then we’ve got a function level scope of age, which is right here. And then we’ve got a nested function level of scope, which is right here, age. So three different levels.

So understand that the value of age cannot be changed in a different scope. It can utilize the copy of that value in the nested function body, but it cannot modify the original age in the global scope or the outer scope. Hopefully this makes sense. So this lecture, as well as the one before it, are paramount to understanding any programming language, really. And so I recommend you watch both of these again and practice with this concept. So let me organize this code a little bit and add some comments. And I’m probably going to add this file to this lecture as a supplementary file, so you can have it and play around with it. Okay? I don’t do it for all lectures, but lectures that are really important where we’re writing a lot of code, I’d like you to have that file.

So I’m going to write the comment here defining a nested function. And this is being defined in the body of this function out here. And then over here is where we are calling. We are calling the nested function. Okay? Now I’ve got a quiz for you. Do you think in the outer, in the global scope, do you think that you’ll be able to call this add four to age? Think about that before answering. Well, if you think you cannot, then you are absolutely right. Because again, where is this being defined? It’s in the body of this function. So you cannot access it externally.

You cannot access it outside of this function. So even if I try out out here in the global scope, if I try to call add four to age, you’ll see that it’s going to error out. It doesn’t know what this is. Unresolved reference. It doesn’t know what this is. So hopefully these two lectures, the one before this as well as this one, you’re able to understand. If not, please rewatch it and practice. The best way to learn, again, is to practice and type along with me and change things around and experiment and go into the rabbit hole, so to speak, as you experiment with this. And then build upon the knowledge, lecture by lecture, coding along.

  1. Section 3 Assignments

Project that I’ve opened up here and head over to section three because that’s the section we’re in. And we have five assignments here to go over, and I’ll cover them in this video. And by the way, section four, five, six, as we proceed in the course, we can have a lot more assignments because there’s a lot more variety of problems that we can explore in those sections that will also apply everything that you’ve learned up to this point, plus many other things. So expect the number of assignments to go up as we proceed in the course because there’s a lot more things to COVID But for this section, we’ve only got five assignments. So let’s get started with assignment number one. It says create a function named Merge lists that accepts two lists as an argument. Okay? And the function is supposed to merge both of those lists together and return the result. So why don’t you try this out on your own and then you can resume to watch my solution, pause the video now.

Okay, welcome back. Hopefully that wasn’t too difficult. So we used a def keyword to define a function, and it’s called Merge underscore lists. And as you can see, it says it accepts two lists. So we need to pass in two arguments, list underscore A and then list underscore B. And here is where we actually do the concatenation of these lists or combining them together. So if you recall, all you have to do is use the plus sign. So you do list A, plus list B, and that will combine both of these lists together and it can return the two lists or the combined list like that. Now let’s use this function. So if I was to use merge lists and pass in two lists here, let’s go over an example. Let’s say we have 1234 in the first list, and the second list can have letters such as A and then B, C and so on. Okay, so we have two lists here, and let’s print out whatever the result is going to be of invoking this function. And you’ll see that the output is going to have this list combined into one list.

All right, these two lists combined into one. So let’s run this and whoops make sure again, we’ve seen this before. You want to right click this file and click on run assignment one because it might just be running some other file. Notice we have assignment five here opened up because that’s something that I had opened up before. So make sure you’re on assignment one, and you right click this particular file and you click on run assignment one. And there we go. Notice we have both of the lists combined into one large list. And by the way, I’ve provided solutions to these assignments. So if you scroll down, you’ll see that the solution is right here. Okay? And I go over sort of an example of running that and making sure that the function works as expected. So assignment one is over. Hopefully that wasn’t too difficult at all. You should have been able to do this.

Let’s move on to assignment two. And here it is. It says, create a function called separate that accepts a string as an argument and returns a list containing each of the characters of the string separated as individual items in the list. Okay, so again, what do you have to do? We have to create a function called separate that accepts a string as an argument, but it returns a list that has each of those characters in a string as separate items in that list. Okay, so also it says, make sure to test this function. So how do we do this? Well, let’s look over at the solution. I’m going to copy it and paste it up above. All right. And I’ll uncomment this so that you can see how to run this. So notice a separate function, it accepts a string, and then you return that string using the list function.

Right. We’ve seen this function before. It turns whatever it is into a list. So we’re passing the string and it’s going to separate each of the characters in the string into a list. And below here, we’re printing an example of invoking separate with hello there as the input. So let’s right click this file and hit run assignment two. And boom. There we go. Each of the characters of the string have been placed in separate items in the list. Notice the space between hello and there is also an item in the list because that is in fact, a character empty space character. So that’s what the list function does. So this should have been straightforward if you remembered what list was. If you don’t, that’s okay. This was an opportunity for you to remember it. So let’s go on to assignment number three. And this is a bit more involved. It’s saying create a function called multimerge that takes a list and a string as arguments. Okay. The function is supposed to return a merge list containing the original list argument, as well as each of the words that are in the string argument, in addition to each of the characters in the string argument as individual items or elements in the list.

So what are we doing here? Well, this multimerge is going to have two arguments. One is going to be a list and the other argument is going to be a string. So it could be a sentence saying, hello, my name is Mt. Oz or something. And what this multimerge is supposed to return is a much larger list that contains all the items that were passing to it, but in a specific fashion. It’s supposed to have the data in the original list as well as the individual words of the string. So if we have hello there, we would see hello, and there as individual items in that list. And then finally, it should also have each of the characters separated as individual elements in the list as well. So he ll each of those characters should have separate should be separate items in that large list. Okay, so hopefully that makes sense. So try this out on your own and then you can resume to watch my solution. All right, welcome back. Hopefully that wasn’t too challenging. It was a bit more involved than what we’ve seen so far, but hopefully you got a chance to practice.

So let’s scroll down and let’s check out the solution. I’m going to copy this code and paste it above. And let’s uncomment these lines by using control slash after highlighting all these lines. So what’s going on here? We’ve got the multimerge function definition, and it accepts a list as well as a string. And so if you scroll down, notice we have the invocation of multimerge. It accepts a list and then accepts a string. So what this is supposed to return is the original list, along with each of the words separated by commas as separate elements in the list, as well as each of the characters in this sequence of characters. Okay, and so what are we doing here? We combine it using the plus sign as we’ve seen before. So we do list eight. This is the original list plus STR split. Notice this split function, by default, splits a sentence or a series of words on whitespace.

 So it’ll take hello, it’ll put that as a separate item in the list. It’ll take my it’ll put that as a separate element in the list and name and so on. And then finally we have this list STR. And you know what this does? This takes each and every single character and puts it into a list. So if we run this, let’s right click and hit run. There we go. This is the solution. Notice it has its one grand list that contains the original list 1234, along with the words hello, my name is MTOs. And then finally, it also has each of the characters as individual elements in this list. Okay, notice you can read this out. Hello, my name is but it’s separated by commas as individual elements in this grand list. So hopefully you got that. Let’s continue on to the next assignment. I’m going to close this file. Let’s look at assignment number four. So this requires us to define a function called last list. All right, that’s the function’s name, and it can accept an unlimited number of lists, but it returns only the last list. If we look at this example, notice it says the below function call should return Mike and John. Now, where’s Mike and John? Mike and John is in the last list of this function call. So this is list one, list two, list three.

So write a function that is supposed to return the last list, and we can have an unlimited number of lists. So try this out and then you can resume.

To watch my solution, pause the video now. Okay, welcome back. Hopefully you were able to do this. So if we scroll down, this requires, by the way, a little bit of slicing. So let’s scroll down and I’m just going to copy the solution here and paste it up here and we’ll uncomment the code and go over what’s going on. All right, so last list is defined here with star ARGs. Okay, remember, star ARGs. This allows us to have an unlimited number of arguments. So it doesn’t have to just be three lists. It could be five lists or ten lists or 100 lists. Okay. And so what are we doing here? It returns. Now, we treat ARGs as a list because this is going to contain each of these values, each of the arguments that were passed. So notice, negative one is going to start slicing from the end of the list of ARGs. And what are the list of ARGs? These are all the list of ARGs. And in this case, we have three arguments. So negative one is going to target this last list right here.

Okay? And then when you do up to but not including the length of the ARGs list, that means the index position for this is going to be up to three, meaning length of ARGs is three. So if we do a 0123, so up to but not including three, that just leaves us with this negative one. That leaves us with this last item like that. Okay, that’s one way of doing it. Another way is if we just do this is negative one, that gives us the last item. I just wanted to show you that you can actually slice it giving two values, not only just the negative one. All right, so let’s run this and you’ll see whoops we want to make sure we test this functionality out. So I’m just going to let’s just copy this code and paste it here. This is the invocation of the list and I’m going to surround this with a print statement or print function call. All right, there you go. So let’s run this right click run assignment.

And there we go. We get Mike and John. And so this is working as expected. Notice this function call should return Mike and John. Now, if you scroll down, I kept it a little bit complex here because I wanted you to think about if we were to pass two values to this slicing, what would the second value be? And so that would have to be length of ARGs. But this would really this is overkill, right? You can just it would suffice to just do negative one because that will, by default, give us the last value. But I just want to prove to you that if we were to also do length of orgs as the second slicing, value here. This would still return Mike and John the last list. Let’s run this. And there we go. Notice. It gives us Mike and John. Now notice it does return. It with a tuple surrounding that list argument. If we were to use this approach, but I think this is overkill, we should just be dealing with negative one as the last element in the ARGs list. All right? So practice this concept and maybe add more values to this list and practice what happens when you change these to negative one, negative two, negative three. Try all those values and get some practice. So that’s it for this assignment. Let’s move on to the final assignment for this section, which is assignment number five.

Now this is a little bit more involved. It says, define a function called key list items that can accept an unlimited number of lists along with another argument. Okay, so if you look at the example here, notice we have the first argument, and then we have these unlimited number of arguments for Things. We have this list for people, we have this list, and we could have had many, many other lists with the given keys. So these are keys such as Things is the key for this list, people is the key for this list. And then the first argument is basically going to be going to be one of the keys. So what’s the deliverable for this assignment? It says the function should return the second to last item in the specific list specified by the user of the function. So let’s say that the user of the function writes code like this to use this key list items. All right? And it says that the second to last item in the specific list specified. So the specific list specified here is people. And so that’s a people list. So second to last item would be jan. Right? Right here. So that’s what this function call should return. Notice it says, for example, the below function call should return jan. Okay, the second to last item in the specific list specified, which in this case is People. If we were to change this to Things, then second to last would of course be book. So why don’t you try this out and then you can resume to watch my solution. Pause the video now. Okay, welcome back. Hopefully that wasn’t too difficult. Let’s scroll down. I’m just going to copy the solution here and then we’ll go over it.

So let’s paste it and let’s uncomment all of these lines of code. So key list items is the function name, and the first argument is the key, which could be, in this example, Things or People. And then notice this is the keyword ARGs, meaning we can have an unlimited number of keyword arguments. In this case, how many keyword arguments do we have? We just have two. We have things and we have people. So what does this function do well, it takes the quorgs at the particular key specified, which in this case, if we were to run this example, it would be people. And so people at quorgs is going to give us the values of people. So this shouldn’t be key. This should be actually values or value list, rather to be more precise. So value list. And so value list at the negative index two position would give us the second to last value, which is Jan. All right. And so here’s an example of trying this function out. So we’re passing people. We have things, we have ages in this case.

So let’s try this out using some of these different keys when we invoke this function. So if we run this now, we should expect this to return Jan, because Jan is the second to last item in the people list. And notice we are specifying people here. So let’s run this and notice it returns Jan. If I was to change this to instead of people ages, then it should give me the number 30, because that is the second to last item in the ages list. So let’s run this and notice it gives me 30 for books, excuse me. For things, it would give me TV. So let’s test that out. Things. Let’s hit run and notice it gives us TV. So this functionality is working as expected. So hopefully you were able to practice these concepts and you didn’t just move on to my solution without practicing. I’d like you to struggle for each of these assignments. That’s really the best way to learn. Don’t just move on to this assignment solution, which is pasted below. I’d like you to try these assignments out yourself.

And by the way, if you had trouble with these, that’s totally okay. What I’ve seen work is if you have trouble this week, you might want to come next week again and try these assignments out even though you’ve seen the solution. But later, after a week, you’re going to forget what we covered. And so that’s the time to actually practice. Learning is about understanding forgetting, understanding forgetting, understanding forgetting. And then the more forgetting that you do and the more repetition that you experience in your learning process, the better you’re going to get at something, and then it’ll stick to your long term memory, right?

 So the assignments in section four are going to be a lot more challenging that are going to involve all of the things that we talked about so far in this course. So section four, you’re going to learn about loops, if else statements, of course, some more functions, and you’re going to learn all sorts of things. So you’re going to be able to apply that knowledge in the assignments that are in section four. And there’s a lot more puzzles to go through in section four. So make sure you learn all of these, practice all these assignments in section three, two, one, watch those videos again and then you can check out section four. Start taking the videos, start learning the videos. And then finally when you have enough practice, section four is really going to challenge you in.