Join Joe Marini for an in-depth discussion in this video Variables and expressions, part of Up and Running with Python.
- View Offline
Now let's take a look at how the Python language works with variables. So I'm going to go over here and open up variables_start.py and here in my snippets I've scrolled down to my variable section. And I'm going to copy lines 22, 23, and 24. So I'll copy those and bring them over. This gives me an opportunity to point out a couple more things about the Python language. First, comments are declared using this character right here. Now some people call this a pound sign, some people call it a hash symbol or a number symbol, doesn't matter what you call it.
You use this to declare comments. The next line, line 6, declares a variable. In this case I'm declaring a variable named F and assigning it the value of 0 and then on line 7, I have print F, which is going to print the value of that variable. So let's Save and let's run this. Okay. And again, if you get a little dialog that says how should I run this choose run pie div. Don't choose the unit test 1. So you can see that it's printing the value 0. Because that's what the value of the F variable is. Okay, let's go back to the snippets and now let's copy lines 26 through 28 and bring those over.
Paste those in. As you can see here I've got f equals 0 and then print f, and now I've got f equals abc, so I'm redeclaring the variable. I'm simply saying whatever f was before it's now a string. So let's go ahead and save this and run it and you can see that that works. In the first instance I've got f equals 0 and then I print it and that's right here, and then I got f equals abc and I print it and that's right here. So even if the variable has already been declared you can redeclare it and that works just fine. All right once again back to the snippets.
Let's copy lines 30 and 31 and we'll paste that and let's uncomment line 14. Alright so save. Now let's see what happens when I try to execute the program here and it should run. And you can see that I'm getting a type error. Cannot concatenate str and int objects. This is happening because Python is what's called a strongly typed language. And even though you don't have to declare the type of a variable before you use it. What happens is, when Python goes to execute the code, if it infers a specific type for a value or variable.
What I'm doing here is using Python's built in function called str to convert this type here into a string. So that way I'll have a string plus a string and that work just fine, so I'm going to save and I'm going to run. And you can see now string type 123 is being printed out as we would expect. And in order to do this, you have to convert types to the same type. So, we're going to str right here. Okay, back to the snippets.
Now, let's take a look at global versus local variables. So, I'm going to copy this right here. And again, and we'll get more into functions as we go on. But I'm going to paste this in for now. Okay and lets go ahead and take out some of the lines we were working with before, because we don't need them for this example. So here on line 10, I'm defining a function. And inside that function you can see that these two lines are indented by two spaces, indicating that they belong to this function right here.
I've got F equals DEF and then print F. Now remember, I've already declared F up here. So there's a variable here and there's a variable here. And back in the snippets, we need 2 more lines. We need 40 and 41 right here. So let's copy that. And let's go down here and paste. Okay. So we're going to call some function and I'm going to print F. So let's watch what happens. going to click Run. You can see that f starts out as 0 and on line 7 it prints f. That's the 0 rate here. Then I declare the function.
Down here on line 15, I call some function which sets the value of f to be def and then prints f. Now in that case, it's right here, that's what's getting printed. And then outside that function, after it returns, I print f again and it's back to being 0. That's because when you're inside a function definition like this, the function gets it's own local copy of whatever variables you declare inside the function. So this variable f right here is different than this variable F right here.
Now if I actually want to affect the value of the global variable I have to tell the function that this is in fact a global variable. So I'm going to use the global statement right here with the name of the variable called F. And now when I run the code, you can see that def is being printed twice that's because it starts out as zero here inside the function assign the value of def. And because I've told it that's it's a global variable now it's going to affect the variable value of it globally.
So after this function comes back and we print f again the value has been changed outside the function as well. So you can see it's being set to def in both places. Okay, and let's go back to the snippets one more time, let's copy these lines right here, and we'll paste them in here. Okay, and we'll save. The del statement deletes the definition of a variable that was previously declared. So up here I declare the variable f, down here I delete it.
So when I get to this line there should be a problem, it should give me an error because that variable is gone. So let's just for clarity's sake get rid of these lines right here, so all we've got left is 6 and 7 and now we've got 9 and 10 right here. So we'll save and we'll run. So you can see that the first thing that happened was the number 0 was printed because that's what f is right here on line 6. Then when we get to line 10, after calling del f you can see that there's an error.
It says name f is not defined. That's because I deleted it on line 9. So you can undefine variables in real time by using the del statement. Okay that's a quick look at using variables in Python.
- Installing Python
- Choosing an editor/IDE
- Working with variables and expressions
- Writing loops
- Using the date, time, and datetime classes
- Reading and writing files
- Fetching Internet data
- Parsing and processing HTML