Monday, January 20, 2014

Introduction - Variables and Interpreters

Now that you have Python set up you can open the interpreter. 

First, it is important that you know how to open the command prompt window. Press the "Windows key + R", type in "cmd" and press "Ok". This will open the "command prompt" or "console" window. This is where most of your early programs will run from so you may want to create a shortcut to it, or get familiar with opening it. 

Once in the console you can open the interpreter by simply typing "python" into the console and press enter. If you installed everything correctly you should get a couple of lines of information followed by ">>>". The ">>>" is an indicator that you are now running the Python interpreter.



The interpreter allows you to write code in real time. That is you can create and manipulate variables without ever having to save your program. To demonstrate, let's create some variables and manipulate them using the interpreter.

Start by typing:
5+7
then press enter.


Notice that the interpreter prints 12 on the next line, which is the sum of 5 + 7. The interpreter is reading your "code" and calculating it.

Now let's try dealing with some variables, then I'll explain what variables are and why they are important.

Type:
a = 35
and press enter.

Now type:
a
and press enter.

Notice that when you input a the interpreter interpreted it to mean 35. What you have done by typing "a = 35" is told the computer that a is a symbol (known as a variable) that will represent the number 35. The obvious question at this point is "Why not just type 35?" The answer is that by having a variable, a doesn't have to represent 35, it could also represent 5 or 4,000. By using a variable we can write a program that takes any value and manipulates it. Let's write a simple program that can act as an example.

Let's create a variable called year and assign it to represent 2014.

year = 2014

Next let's create a variable called age and assign it to be your age.

age = 300

Now, let's find out the year that you were born:

year - age


Now I can see that I was born in the year 1714. Your results are probably different than mine, but that is exactly the point. Because we used a variable instead of a "literal" number, the program will work for anyone, not just people who are 300 years old. Without variables I would have to write a program for people who are 10 and another one for people who are 11 and 12 and so on... Then all of those programs would only work during 2014, then I'd have to write a new set of programs for 2015. So, we can be thankful that variables exist and we only need to write one program.

Of course, this program is not very useful since it doesn't take into account actual birth date which will cause it to be off by one year depending on whether the user has had a birthday this year or not, but it demonstrates the power of variables.

There is a lot more to variables than I have mentioned here, which we will eventually get into, but for now I wanted to make sure that you understand what a variable is and why they are essential to writing programs.

Play around with the concept of variables to familiarize yourself with their use. Try assigning a number to a variable and then assigning a different number to the same variable. See if you can create code that "breaks" or doesn't work properly.

Next time I will go deeper into what variables are and how they work and describe different types of variables and what they can each be used for.


No comments:

Post a Comment