Some Foundations: Variables and stuff
July 7, 2010 7 Comments
In some of the earlier tutorials I’ve been using variables without actually explaining what they are. Strict Python-Zen: within Python there are no variables everything is an object*. On the assumption that that is too abstract for you, I will explain it a different way, through the use of containers (like trays and buckets) which, if I’m lucky, will preserve at least a little bit of the Python-Zen.
Variables allow you to be flexible. They are a way of storing something in the computer, much like having a bucket for putting your Lego in. If you need to go do some chores and just drop your toys, you might not be able to find them again later – or if your parents clean up the toys might get thrown out in the course of garbage collection. If, instead, you get a plastic bucket out and put them in there, you can rest safe in the knowledge that they’ll be there later after you finish your chores. Variables are a bit similar. They’re somewhere that you can put data that the program is using and label it for future reference. Just like a bucket with the word “My Toys” on the side, you know that, assuming you put it in there earlier, your toys will be in there next time you look.
Indeed, the name you put on the outside of the bucket doesn’t really affect what you put in it. You could have the words “toy cars” on a bucket and yet put all your trading cards in there instead. The point is that the bucket is a safe place to keep your toys. Variables are a safe place to keep the program’s data.
When you do something like this:
a = 1
It is like putting 1 into a bucket marked a. You can call variables almost anything – their name can’t contain spaces and can’t start with a number. There are different conventions used to name things which really should have spaces in them, typically by capitalising a letter:
>>> 2a = 1 File "<stdin>", line 1 2a = 1 ^ SyntaxError: invalid syntax >>> a b = 1 File "<stdin>", line 1 a b = 1 ^ SyntaxError: invalid syntax >>> myToys = 1 >>> myToys 1 >>> mytoys = 2 >>> mytoys 2 >>> myToys 1 >>>
There are a couple of things happening here that I want you to notice. First up are some examples of names which don’t work. These are 2a (starts with a number) and a b (has a space). Second I’ve shown another variable name which has a capital in the middle of it (myToys). Do you see how the middle capital helps you to betterUnderstandWhereTheWordsAreSupposedToBeSplit? – that is hard to read, but not as hard as betterunderstandwherethewordsaresupposedtobesplit. I’ve also shown you another variable name with no capitals in it (mytoys). Can you see that myToys and mytoys are different buckets? If you put something in the myToys bucket, you won’t find it in the mytoys bucket and vice versa. Variable names are said to be “case senstive”, because whether you use upper case or lower case matters.
Also, you can’t give variables names which are the same as words in the Python language:
>>> for = 1 File "<stdin>", line 1 for = 1 ^ SyntaxError: invalid syntax >>> if = 1 File "<stdin>", line 1 if = 1 ^ SyntaxError: invalid syntax >>> while = 1 File "<stdin>", line 1 while = 1 ^ SyntaxError: invalid syntax >>>
With buckets you can put different toys in them at different times. A bucket marked “Cars” might have cars in, or equally it might have some other collection of stuff in it. The bucket itself doesn’t have a character. It just holds things. So, you can put different ‘types’ into a bucket without causing any ruckus:
>>> a = 1 >>> a 1 >>> a = 'hi there' >>> a 'hi there' >>>
In this example, we first put a number (1) into a. Then we put a string (‘hi there’) into it. In some programming languages this causes a problem, but it doesn’t bother Python.
Finally, I should demonstrate to you that the variable stores the data:
>>> a = 'This is some data. Will it still be here at the end of the program?' >>> for i in range(5): ... print 'doing stuff, time number ',i ... doing stuff, time number 0 doing stuff, time number 1 doing stuff, time number 2 doing stuff, time number 3 doing stuff, time number 4 >>> a 'This is some data. Will it still be here at the end of the program?'
The program first put data into a, then did some stuff, then saw that the data was still there, just as we’d expected. Buckets and other containers play a vital role in keeping your room, the living room, the dining room, the kitchen, the bathroom, the library, the sun room, the garage, the front yard, the back yard, the attic, the space in our bedroom that you sometimes seem to think is yours, that area beside the stairs, and the family room clean of your toys (which seem to have a life of their own in the way they spread themselves around the floor). So too, variables are extremely useful and play a fundamental role in all Python programs other than the most most basic.
* In Python, variables are actually references to objects. They are an index telling the computer where in storage to find an object. This is why you can change the ‘type’ of the thing being ‘held’ by a variable because it’s not really holding anything, it’s just pointing somewhere, and you can change what it’s pointing at.