VARIABLES & ASSIGNMENTS

Variables are one of the most important concepts in programming, and you will almost certainly be using them in every useful program you ever write. Variables are like labels, or names, for the numbers, strings and other values in your programs.  Variables are used for keeping track of all the facts and figures you use throughout your code. Variables can be given any name you choose (although there are some rules to that!).   Choosing good variable names makes it easier to write your code, to change your code later and to help others understand your code (which is important when writing code in groups).

The definition of "variable" is something that can vary (change), and that's what program variables do  throughout a program.  To assign a variable a value, you use the equals sign in what is called an assignment statement, which looks like:

and to change it to a different value, you also use an assignment statement,

The information that a variable holds could be a number, a phrase, the results of a calculation, a list of information or pretty much anything you want that variable to hold.

Variables Can Hold Numbers

For example, in the previous project, we calculated the number of seconds in a day by multiplying the number of seconds in a minute by the number of minutes in an hour by the number of hours in a day. I we wanted, we could rewrite that calculation to use variables instead.

Let’s start by creating a variable called “sec_in_minute” that will hold the number of seconds in a minute (which is 60):

Note: When naming variables, you can use any combination of uppercase letters, lowercase letters, numbers and underscore characters (_). The only rule is that the variable name must start with a letter. It’s good practice to use variable names that make your code easier to understand and read.

Now, let’s create two more variables:

It’s probably pretty obvious (that’s the goal!), but min_in_hour is the number of minutes there are in an hour and hour_in_day is the number of hours in a day.

We could now change the code we did to do our calculation of number of seconds in a day to the following:

And here is what the output would look like if you ran that code:

86400

Variables Can Hold The Value of Other Variables

Remember I said above that variables can hold almost any type of data – that includes data from other variables. We can take our program one step further by creating a variable called sec_in_day and using it to hold our calculation for the number of seconds in a day:

Now, if you want to output the number of seconds in a day, you can simply print the number being held by the sec_in_day variable:

This is what the code and output would look like:

86400

You’re probably thinking to yourself that using variables took a lot more time and a lot more code – in fact, your program went from one line to five lines! But, if someone were to look at your original code:

They’d have no idea what it meant or what you were calculating. But, if someone saw your final piece of code:

They would very likely know exactly what was going on in.

Also, while it seems like we’ve made our code longer and more complicated, we’ve now made it easier to use our calculations to do additional calculations if we wanted to. For example, let’s say we wanted to calculate the number of seconds in a month or a year?

Using the code we’ve already written, it would be easy to add these calculations. To determine the number of seconds in a month (assuming a 30 day month), we can just multiply the number of seconds in a day by 30 – and we can give the result its own variable:

To determine the number of seconds in a year, we can multiply the number of seconds in a day by 365:

As you can see, but using variables to hold our first calculations, we make it easier to do additional calculations. And, if we’re working on a team or in a group, the entire program is easier for others to follow and understand.

Let’s put this all together, and create a program that will calculate the number of seconds in a day, month and year, and print out the results:

946080000

The real benefit is that variables can change throughout a program…

The final benefit of using variables is that, if a variable needs to change, we can change it in one place as opposed to having to change it in lots of different places. For example, if scientists decided tomorrow that there should actually be 61 seconds in a minute, we could change our first line of code to:

And all of our code would now continue to make accurate calculations. If we wouldn’t have used variables, we would have had to go through our code and change every instance of 60 to 61 where we were referring to the number of seconds in a minute.

Of course, we don’t expect the number of seconds in a minute to change, but there are plenty of things in this world that do change, and being able to update your programs quickly and easily when things change is a huge benefit to programmers.

Variables Can Hold Strings

In addition to holding numbers and calculations, variables can also hold strings. For example, we could have a variable that holds one of the output message we’re using above:



We could then print that message to the output, along with our calculation:


Here is what the code and output would look like using a variable for the text string:

Number of seconds in a day: 86400


Note that because message is now a variable, we don’t put quotation marks around it.