Variables in Python

Hey, second tutorial!

For the basics, go ahead and watch this video:

If you've already watched it, then here's some more information you could use:


You need to avoid using keywords as names of variables. As per Python 3.8, there are a total of 35 keywords. I'm putting a list below but you should know that no one actually memorizes the whole list. These are all very common words that you will use during coding and as you progress, you will automatically know which word is a keyword and which isn't (not to mention, your IDE will kill you with errors if you do accidentally use a keyword).

Once you're done reading through these, let's move on to the fun stuff

At the end of the video, I asked you to run this bit of code and see what happens:

Your output should look something like this:

Now, the question is what exactly happened when the code ran?

NOTE:

There is something called a "dry run" in the world of coding where we basically pick up segments of code and try to determine what will happen when the code is run. While learning, try to do a dry run of everything you code as it will help you develop your logic. Once you start coding on your own and creating things, you will use dry runs to figure out where the bugs are and how to fix them.


Anyway, coming back to the point at hand: let's see what happened in the code I gave you guys.

In the first line, we created a variable "a" which would point to a memory location containing the value 15. When we print "a" in the second line, our program simply looks at the value "a" is pointing at and hence, displays 15.

Now, we create a second variable called "b" but this time, instead of giving it a new value, we set it equal to our first variable "a".

On doing this, the program looks at the value in the memory that "a" is pointing at and assigns the same memory location to "b". For this bit, "a" and "b" are both pointing to the same exact location in the memory, i.e., 15.

In the next line, we reassign the variable "a". A new location is created in the memory which has a value of 16 and "a" now, points to that location. Remember, "b" still points to 15. Only the value of "a" has changed.

So, now when the final two print statements run, we know that "a" corresponds to 16 and "b" corresponds to 15. Hence, the output:

If you gave it a shot and tried to explain the code to yourself and it looked anything like this, great job! Keep practicing and trying out different things and be sure to drop me an email if there is anything specific you want me to cover in future tutorials or you just have a question in general.

Aight that's all for today. Cya