Programming – What is a Variable?

Now variables, they make programming interesting.

Think of a variable as a box. Variables have a name and a type and they store data inside of them. This data can be anything from a piece of user input – for example their name – or the result of a calculation.

If your variable as a box, imagine you write on the box describing what you are going to put inside it. That’s the name of the variable. Now, this is a cardboard box so we don’t want to put anything heavy in it. Let’s make this a ‘teddy’ type. Any teddies can now be stored in their but only teddies. The name we’ve written on it is myTeddies.

Types of Variable

OK, so confession time. Teddy types don’t exist in the world of programming. But it helped you understand the analogy, right? Variables have names, types and can store stuff (data) inside of them. Although types vary by programming language and platform the common types available across most (even if they have a slightly different name) are:

TypeDescriptionExample
IntegerThis data type stores whole numbers1, 2 or 3
Real (or Float)This stores decimal numbers1.23, 4.113, 777.55
CharacterThis stores a single (1) alphanumeric character or symbolA, 2, *
StringThis stores one or more alphanumeric characters or symbols (including spaces)Aaaaaa, Two, 55, Hello World!
BooleanThis store either the value TRUE or FALSE.1, 0, YES, NO, ON, OFF, TRUE, FALSE

Data types can be explicit or implicit. For example, in Python a data type is implicitly a string by default. Most programming languages state that is preferable to explicitly set your data type, even if the implicit one is correct to assist with data validation and to ensure the correct input and values are stored there.

Naming Variables

You can usually name variables with very few restrictions. The common restrictions are:

  1. No spaces
  2. No special characters – for example, ?, !, &
  3. Not starting with a number – for example 1variable is not good, my1stname usually fine.

Having said that, choosing a sensible name for your variable is key. It allows you to recognise it as well as other people who may be working on your code.

Consider this piece of Python code:

myVariable = input("Enter stuff")

Now, if I asked you what this program did what would you say? Funny answers in the comments please!

If I show you the same code but this time with an appropriate named variable:

yourFirstName = input("Please enter your first name:")

See how much easier that is to read? You know your expecting the first name of the user to be entered in there just by taking a casual look at the code. This helps later on when you use the variable as you’ll quickly lose track of what variable1, variable2 and variable3 actually mean in anything more than the shortest of programs.

Assigning a Value

Assigning a value to a variable has quite common syntax among most programming languages using the ‘=’.

myCalculation = 1 + 1

Don’t let Maths confuse you here though. The sum we are performing written as we would in a Maths class is:

1 + 1 = 2

If I take the code above and write what the value would be rather than the variable name it would look like:

2 = 1 + 1

Remember the order for writing a variable assignment statement:

  1. Variable name first.
  2. Then an equals sign
  3. Then your calculation, input function etc. Basically, whatever you are going to store in there.

Final Thought

Variables are called so because their values can change – or vary. You can replace the value in your variable as many times as you like by using the assignment statements above. Just remember, once you’ve replaced the variable value the old value is gone!

Happy coding! Any questions, throw them in the comments below!

You may also like...

0 0 votes
Article Rating
Subscribe
Notify of
1 Comment
Inline Feedbacks
View all comments
trackback

[…] we looked at What is a Variable and one of the things we discovered variables are used for is storing user input. Most of the […]

1
0
Would love your thoughts, please comment.x
()
x