. In the last lecture, Scott kind of described the basic principles of event driven programming. In event driven programming, event handlers respond to events that arise whenever you hit a key, you click a mouse, you click on a button. In event driven programs, the various handlers need to share a common set of information that allows you to make your interactive program run. In this lecture, I'm going to talk about one technique for making all these handlers work together. That is, we're going to have them share a common set of global variables. Now, up to now, we haven't distinguished between global and local variables. In this lecture, I'll describe the difference between the two. I'll show you some examples of where we can use, and maybe should use, global variables, and I'll show you examples where we shouldn't use global variables. So, maybe this will give you a little bit of background behind how we're going to approach the first few weeks of class. So let's talk about global and local variables in Python. So you're already familiar with building global variables. You've done it a fair bit. Anytime you define a variable outside of Python functions, you're building a global variable. So for example if I say num one is equal to one. . I have just defined and used the global verbal num one. Now it turns out you've actually built local variables also, you just didn't know that. When you built rock, paper, scissors, lizard, spock, you created for example a local variable to hold the computer's guess. You then later on used that local variable to do some further computation to figure out who won. Variables that you create inside a function are called local variables. So lets go through and make an example. So I'm going to define a functioned fun. And I'm going to define a local variable num two and we are going to make it num one plus one. And let's just print out num two. And then we will call that function some, let's say fun. And let's run that and see what happens. So when we call it fun. It says, let's make a local variable num two. And should, value should be the value of the global variable. We can use this global variable in here, num eleven. + One. So then we ask it to print it out. So it printed out two. Now, you might ask, well, what's the distinction? I don't see any difference here. So let's just do a little test. So down here, after I've run the code, I'm going to now print out the value of num one. And if I do that, well, num one is still one after I ran my function. Okay, no, no mystery there. Let's just see if we can get the value of num two. So, I am going to say print num two. This is error num two not defined. So the critical distinction here is that any variable you create inside a function can only be modified and used inside that function. It's local to the function. It doesn't exist after we've finished calling the function. We can't access it. We can't share it between two different calls of the function. It's created once inside the function. You can modify it in there. You can use it, but it goes away when the function call ends. So, that's the definition of why we call it a local variable. On the other hand, num one always exists. It's there for the entire run in Code Sculptor. We can use it inside functions, we can use it outside functions. Now, this leads to one, one interesting question. So what I'm going to do is, I'm going to I'm going to get a modification of my function. I'm going to do something you shouldn't really do. But I want to point out something interesting about how, Python works. So I'm going to go through in here and I'm going to try to do the following. I'm going to go through, and I'm going to make this variable called num one. And I'm going to define it to be two. So now I'm going to run my code. Let's see what happens. So, I'm going to postpone this for right now, Run it. And what happened? So we printed out num one. It was one then we ran this function, and inside there we said, num twelve. Is eual to two. Then we said num one is equal to NUM1 + one. It printed out three, then we printed out NUM1 again. Hm. A little strange. Well, let's see, inside the function, it said NUM1 is equal to two. That seems to be understandable. We changed the value of NUM1 to be two. Then we said, well, NUM2 is two + one, that's three. We got a three but it print out, It said num1 is one here, What happened? We went through, and we said num1 is equal to two. We created a new local copy of num1. So, it's not really good to reuse variables like [inaudible]. There's a global num1 here, and there's a local num1. So, I made a second local num1 and actually changed its value to two. When the function finished executing, that local version, disappeared. And when I asked to print the value of num one down here, well, the global variable still exists, and so, it printed out a one. So the critical thing to understand in here is that if I try to actually go through and even modify a global variable inside this, in front of here, it's impossible. I can't do it, at least the way I've done it right here. So, the critical thing is that anything you create in a side, inside a function is going to be a local variable. So let's do an example where we construct a function and use local variables inside it. So the example that I want to consider, is this problem of converting Fahrenheit to Kelvin. So let's make a function that does that. So we're going to do that, and we're going to use two local variables in here. So let's do it. We'll say define Fahren to Kelvin. Just going to take the Fahrenheit, as parameter and we're going to create two local variables. What is going to be, the value if we converted from Fahrenheit to Celsius? We'll, hang on to that for a second before we try to convert to kelvin. So, let's see, we can say Celsius is equal to five-ninths times, let's see, the Fahrenheit minus 32. And then what do we need to do? We need to go through and add something to that temperature in Celsius. And what we need to do is add, and we need to add the, the temperature in kelvins of zero Celsius. So, let's just make a constant that does that. And I think that was 273.15 so with those two local variables, we can now actually compute the answer very easily. We can say return Celsius plus zero Celsius in Kelvin And let's actually just print out maybe one test call real quick here. Let's do it for 32 degrees. So we'll run that, since we have this. Oh, look it there, missed an underscore. So it says that 32 degrees Fahrenheit, that goes to zero degrees Celsius which, when you add in the magic number of 273.15 goes to 273.15 degrees Kelvin. Let's test it on one more value real quick. Let's do it on boiling, so boiling is 100 degrees, 100 degrees larger. So let's talk about when you should use a global variable versus when you should use a local variable. In practice, you should always prefer using local variables unless you have a specific reason not to. Let me give you an example, imagine you're building a software system for a new modern airliner. You have to write the flight control system and you also have to write the in-flight entertainment systems. If you decide to, hey, I'm going to make everything global, I could easily see a situation arise where you have a variable dial in your flight control system that maybe is, I don't know, some instrument dial. And you could have a variable dial inside your entertainment system that might be, maybe the reading of one of the dials that the passenger uses to control the audio. I think you can agree that it wouldn't be a good idea to do this because you don't want for example, one of the passengers changing his audio settings to then cause one of the instrument dials to change in response. So that's a place where it's clearly in your best interest to use local variables to capture information that's only local to each of these systems. Using global variables is, also gives you kind of the ability to really introduce fairly mysterious behavior. So let me show you with this example. So here, I have an example where I've made this variable numb. It's a global before. Then I've defined two functions. Fun1 and fun2. I've hidden their bodies. And then I've printed out the value of num, before I call fun1, after I call fun1, and after I call fun2. So what you can see here, the values are four, five, and six. Now, I told you beforehand that if we tried to reference num inside fun one or fun two, Python automatically created a local variable, that only existed while we were executing fun or one or fun two. And we couldn't even modify this global variable num. So, how did I do that? Well, Python has a facility to do this. What you do is, if you would like to access the global copy of num. You have to go through and declare num to be a global. You say global num. Now, any reference or any definition of num afterwards, modifies or uses the global variable. Note, you don't have to say global if you're just using a global variable. You only really just say this if you actually want to update a global variable. So here, you define num to be global. We updated num to be five, that's where we got five here. Then here we defined num to be global, we updated to six, that's why num was six here. So global variables have ability to both be dangerous, introduce kind of a mysterious side of things. That's why they're tough to program with so why do we choose to use global variables in our introduction to event driven programming? They're the simplest way for the various even handlers to communicate data between each other. That, when you're building Guess the Number, you're going to have to have an input handler and a couple of button handlers. Those are going to need to be able to modify common variables. Using global variables is kind of the simplest way to do this, it has the least programming overhead. When we get to object oriented programming there are other techniques that give you a better way to hide information and control how much information is visible. And we'll talk about that a little later in the class but it requires a fair bit of sophistication, a lot more sophistication than you will have the first week of class so our overwriting goal is to get you writing a driven code that builds cool interactive applications as quickly as possible. So, we want you to use global variables but just use them sparingly and know when you're going to use them. I'll see you next lecture.