Wednesday, July 27, 2011

Variable Fighter : playing with snakes part 2

I feel in a programing mood today so I'll be continuing the playing with snakes post I made a few days ago. Last time we played with some input and output in the shell. Today we move onto some variables garnished with a little scope concept.

First thing first, if you hated algebra this is like that only you don't have to solve things. You get to say what x is and everything else bends to your will. Though if you name your variables x you are going to make anyone who reads your code cry. So first thing first there is a called typing for variables. Where a variable is of a certain type, such as a string or int we used before. In other languages you would need to declare variable types, for instance in C languages you would do things such as 'int your_int" or "string your_string". Once the type is declared it won't change, if you try to assign something else it'll cause an error.

Assigning is the basic point of a variable, just having a type there makes it sort of pointless. It's very simple and as I said before like Algebra but you get to say what x should be. You simply have your variable and put '=' after it followed by what you want the variable to be. It's horribly hard to for me to explain it, so I'll have you do it, and you'll see it.

So get IDLE up and running and type the next few things.

name = "bob"
age = 20
print name
print age

More lines of code than I've ever had you done yet. Before we did everything on one line, now it's over multiple lines. You assigned "bob" to name before you assigned 20 to age and yet when print was called with name after it bob was printed, same with the age variable. This has something to do with an idea called scope. Scope is where variables exist, and where they cease to exist, but I'll get more into that later. Next I'm going to show scope not working.

Please type the following

print date
date = "July 27, 2011"
print date

Didn't work out did it? We got the wonderful red error. Yet we were able to establish the we assigned a string to the date we were able to print it out. Before the point in time where we enter 'date = "July 27, 2011"' nothing exists for date. Nothing. Anything referencing to it before we put that line down it a horrible thing that will cause your program to fail. The only reason we were able to keep typing is because we are running everything in the IDLE shell. If this were actually run as a stand alone program through the standard python interpreter you would not get the second print date to fire off, it'd be dead at the first one. While not a hard concept it'll creep up to bite you sometimes.

Now onto the next thing. Variables are reusable, they don't disappear after you use them once. For instance lets show off name and age again in a worthwhile manner with a little string concatenation. Please do the following.

print "Hello my name is "+name+" and I am "+str(age)

Just like before the str method attempts to turn anything into a string as only strings can be concatenated together. Now lets move onto the next part. Variables are well variable, you can reassign what they contain at any moment. For instance the following.

name = "rick"
print "Hello my name is "+name+" and I am "+str(age)

Bob has magicaly become rick. Now that was fun, lets change his name to Ron.

age = "Ron"
print "Hello my name is "+name+" and I am "+str(age)
fun fact I did something out of order and this is hiding it

No that was not a typo. That was an example of python not caring about type when it comes to variables when it can attempt to process them. Any variable name is fair game to be changed to any other variable type. This can cause a lot of debug problems. Like if you think you correctly changed a variable but spelled it wrong, it just makes a new variable in scope instead of replacing the old one. It'll become a possible pain when I get to objects later but I think this is enough for right now.

Next time I'll introduce the concept of methods so we won't have to type, or copy paste, 'print "Hello my name is "+name+" and I am "+str(age)' so many times.


  1. Scripts are so deceptively simple, a small pool of easy concepts to understand and manipulate well, then bam, characters.

  2. Simple as they might be, it takes some time to wrap your head around them.

  3. It does take a while to wrap your head around them yeah, I was taught a few simple programs in school but I've forgotten how to do them :( Thanks for the tutorial though :)