This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

"Variable" Jones and the Temple of Doom


What you didn’t see in the last example is an equal sign “=”. That’s because in python and programming in general the equal sign is what’s called an assignment operator. We use it to assign a value to a variable.

This is really the first time I’m introducing a concept that you need to know how a computer works to get. You can get by on algebra, but eventually you need to know that a = 7 is the same in programming and in algebra, but in programming there’s a little bit more involved.

In programming, the value of a variable can change. It’s not really an absolute like in algebra. It’s really a space the computer sets aside in memory. This becomes important when you are starting to make programs that have modules because some of your variables will be used by the whole program (global) and some will only be used by the module (local) and you have to figure out which one you need, and sometimes how to pass them from one to the other.

But the assignment operator, “=” basically assigns a value to a variable. So something like a = 7 is telling the computer to put the number 7 in memory for a.

Try the following:

You see how that works? Let’s step through it:

Is that neat or what? Did you notice how I did that with putting the text in the quotes and combining them with the variables? If so you may have noticed that there are different combinations of spaces in the result. Sometimes there are one space, sometimes two and sometimes three. This is because in python the white space in your code is significant. Python will read it, and it could cause errors. So instead of 55_=_ _48_+_ _7, how could we get 55_=_48_+_7? How about 55=48+7? (no spaces)

Don’t ask me, go experiment… The guys at codeacademy built that thing, use it.

One last thing about variables. You get to make up the names. This sounds cool, but really if you don’t do it right other programmers won’t play with you. In python and most languages you aren’t allowed to use spaces, like “this is a variable”. Not an acceptable name.

You have to use an underscore “_” or what’s called “camel case” where you make the start of a new word a capital letter like “thisIsAVariable”.

Task Discussion


  • LostinLA   June 15, 2012, 11:13 p.m.

    Remember to do the above examples I am giving in python in the language of your choice, such as one of the following:

    Javascript

    Java

    C++

    C#

    Ruby

    Or whatever language you are studying...

    Then look at any others your fellow may have posted, try to find the underlying deep structure behind all languages.