Variables in the world of programming are a way of labeling and storing information so that info can be used again and again later in a program.

Say you want to write a program to calculate the sum of two numbers. Where you first ask the user to input each of the two numbers and then calculate and print the sum. You’ll need to use variables to store the user input. And then add the numbers together. We’ll use a little Python code to illustrate.

What you would do is first ask for some input like this:

num1 = input("num1: ")
num2 = input("num2: ")

And by doing this you are storing the input into variables. The first input (line one) goes into the num1 variable. The equals symbol “=” tells your program to bring the user input into the variable.

And the second input (line two) goes into the num2 variable. Again the equals symbol stores the value to the variable.

Then with an additional two lines of code you would add the two variables together and then print the sum:

sum = int(num1) + int(num2)
print(sum)

Variables can have different types

What does that mean? A variable typically has what we call a “type” meaning it is specializing in storing a certain type of information.

Conceptually, if a variable were like a container you might have a box to store general items in your house. You might also have a lunch box with two or three compartments for storing different food. Different container types each with a specific purpose.

And in variables in programming are not that much different:

  • For example, you might have one variable that is storing an integer (number) like 1, 2 or even 100. So that variable will have a type of “integer“. Whatever you want.
  • Another variable might be of type “string”. Meaning it stores a string of characters like “gowithcode”.
  • And there are other variable types including types you could custom create that specialize in storing data in any way you want.

The other main reason we have different types is to efficiently store these different types of information (integer, string, etc.). And later be able to operate on the information.

Variables can change their type

If you notice in the above programming example, we first asked the user to input a couple of numbers. However the input function (the “input()”) returns a string and not an integer. Meaning the user thought they were entering a number but our program thinks it’s a string – because it doesn’t yet know any better.

So, we tell our program on the third line to convert the two variables (num1 and num2) from string into integer (that’s where you see the “int()” part of the code) and immediately add the two integer values together to get the sum.

Otherwise if we did not first convert the two string variables to int and then added, we would not get a correct sum and possible also receive an error in some programming languages.

What to be aware of regarding Typing

Some programming languages, like PHP for example, give more flexibility to create variables that can be of any type and allow you to later change the type without explicitly telling the program to do that. This is fun in it lets you maybe get more done with less code. But dangerous in that your program may behave in unexpected ways because you aren’t in control, your program is 😉

While other languages like C# require (or at least strongly encourage) you setup a variable’s type ahead of using the variable and won’t let that variable change type unless you explicitly tell it to do so. This approach may lead to more predictable behavior when the code runs but may also require more code to get stuff done.

There are pros and cons for taking both approaches.

Variables have Scope

Scope means where a variable lives or can be accessed from. A program can have different scopes.

Let’s see what that means.

For example, in our above program, we were able to access and add the two variables together to get the sum. However, in many programming languages, including Python, it’s very common and useful to setup variables that will only live for a short period of time and only for as long as they are needed. That way they don’t live long and take up memory space.

An example of this is something like the following Python code:

a = 2

def myfunc():
    a = 1
    return a

print(myfunc())
print(a)

We first define a variable “a” and set it equal to 2. This is the main scope or level where variables can exist.

Next, we define a function “myfunc” and create a variable “a” equal to 1 and return that variable’s value. Within the myfunc function we have a new scope or level. By assigning a = 1 we are only giving the “a” variable that value while within myfunc. And once back outside of myfunc, a will be equal to 2 again.

Then we call myfunc and print what it returns, meaning we print 1 to the screen.

But then the next line we print the variable “a” which prints 2 and not 1. That’s because the scope of the “a” variable is having a value of 2 meaning it knows nothing about the other “a” variable inside inside myfunc.

In Python, however, myfunc will have access to the “a” variable outside of it. So if we were to remove a = 1 and just return a, we would actually return 2 since myfunc can access the “a” variable outside of it.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>