Introduction to variables
In programming, we have a way of storing values so that we can reuse them throughout our code or change them, if necessary. This concept is known as a variable.
Creating a variable is a way of storing a piece of information with a specific name in a computer program. By storing data in a variable and giving it a name, we can easily reuse that data over and over again in our code. Using a variable allows us to also easily change data throughout our code. And labeling a value with a descriptive name allows your code to be understood more easily.
Variables are an important first step in coding because with variables we can start using an important programming tool: and that is repetition. Rather than writing out a piece of data every time we need it, we write it out once and the computer remembers it and can repeat that information.
Reusing Values in Variables
One reason we use variables is that they allow us to easily reuse data in different parts of our code.
When we reuse a value, it will appear in multiple places in our code. Re-typing that value can be a lot of work, can lead to errors, and without a variable name, it may also be unclear what that value is meant to represent.
Let’s say we have a number that represents that we reuse multiple times in our code in order to make some calculations:
156789885 156789885 * 2 156789885 / 4
Rather than writing the same number over and over again, we can save it to a variable named
profit and just refer to the variable profit later in our code when we make the calculations.
We could do the same thing for a piece of text we want to show multiple times. Let’s say we want to show the company name throughout. We can store the name in a variable named company_name that will represent the name of the company throughout the code:
company_name = "NewCo"
You may be thinking, “But what if my variable name is longer than the value it stores? What’s the point of a variable?”. When we use a value without assigning it to a variable, that’s known as hardcoding. While it’s sometimes faster to initially hardcode values in your code, in the long run you’ll run into trouble — especially if you need to change what those values are.
Changing the Value of a Variable
The strong selling point of using a variable is that we can easily change their value, making our code flexible. As we saw before, we can save a number to a variable and then reuse it throughout our code:
profit = 156789885 profit * 2 profit / 4
We could easily switch out the value of
profit, without having to change that number in multiple places in our code.
profit = 1000 profit * 2 profit / 4
We could also change the value of
profit part way through our code:
profit = 1000 profit * 2 profit = 3000 profit / 4
Putting it All Together
Lets recap why we use variables in our code:
- Variables allow us to store information
- Variables allow us to quickly reuse data in our code.
- Variables let us easily change data in our code.
- Using variables with descriptive names improve readability
Updated almost 2 years ago