- [Instructor] Now let's take a look at how the Python language works with variables. So in your editor open the file named variables_start.py. So first note that comments in Python are declared using the hash character. Let's go ahead and declare a variable, so I'll write F equals zero and let's print it, all right, print F. So in this case I'm declaring a variable named F and assigning it the value of zero and next I call the print function, which is going to print the value of that variable.
So let's go ahead and save and run this program and remember if you're not using VS code you can just run this in your terminal. So I'll just go ahead the debug view and run this, show the output window, and you can see that it's printing the value zero, because that's what the value of F is. All right, so let's add a few more lines of code. So I'll write F equals ABC and then once again we'll print F. Now I'm saying that whatever the value of F was before, it was an integer before now let's make it a string.
Let's go ahead and save and run again and you can that that works. In the first instance I've got F equals zero, I print it, and then I got F equals ABC and then I print that. So even if the variable has already been declared you can simply redeclare it and that works just fine. Now let's make another change, I'm going to write print, this is a string plus one, two, three. Okay, so let's save and let's run.
You can see that I'm getting a type error, right. It says you cannot concatenate stir and int objects. This is happening because Python is what's called a strongly typed language even though you don't have to declare the type of a variable before you use it. What's happening is when Python goes to execute the code it infers a specific type for a value or a variable. You can't go back and change the type or combine it with other types. So here I've got a string and I've got a number, which are two different types.
So now I have a string plus a string and that should work just fine. So I'll put a space in there, save it, and then I'll run it and now you can see that the output is what we expected. Now let's take a look at global versus local variables. I'll start by creating a function and we have seen this already. I'll call it some function and inside the function I'll give it some code. I'll say F equals D-E-F and print F.
So I'll get more into functions later in the course, but for the moment just bare with me and let me comment out some of the previous examples, because we don't need them. I'm defining a function and inside that function you can see I've got these lines indented indicating they belong to the function. I've got F equals D-E-F and then print F. Now remember I've already declared F up here, right. F is zero, I didn't comment that one out. So now let's add a few more lines. I'll write some function and then print F.
So we're going to call some function and then print F and let's watch what happens. I'm going to save and run. So you can see, so F starts out as zero, right and it prints out D-E-F inside the function and then it prints out the zero, because these two Fs are different. The reason is because inside the function the function gets it own local copy of whatever variables you declare inside the function. So the function, this variable F, is different than this one right here.
They're considered two separate variables by the Python interpreter. Now if I actually want affect the value of the global variable I have to tell the function that F is in fact global. So let's go ahead and make that change. So right at the top of the function I'll write global F. All right, so I'll save and now I'll run again. Now you can see that inside the function D-E-F gets printed and D-E-F gets printed again, because this function here is now operating on the global variable.
Since we told the function that F is now global this assignment affects the F that's outside the function. So let's make one more change. Let's write del F and then print F. The del statement deletes the definition of a variable that was previously declared. So when I get to this line there should be a problem, because that variable is gone and so let's go ahead and save this and let's run again.
And you can see that D-E-F gets printed out, so the two pieces of code that are still there are working and you can see here now it says global name F is not defined and that's because I deleted it. So essentially what this means is that you can undefine variables in real time by using the del statement. All right, so that's a quick look at variables in Python.
- Installing Python
- Choosing an editor or 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