What The Hell Are Variables?
Well, simply put “variables are names you give to information that matter”. Makes sense? Try to imagine a scenario where you are asked to create a file containing basic information about yourself (name, age, gender), you would write something similar to this:
name: John Doe
age: 24
gender: male
In the above file, you have created three variables
- name
- age
- gender
We call them variables because of their ability to change at any given time. So by next year, your age would be age + 1
, right? You get the logic!
Variables In Computer Programming
Variables are the names you give to computer memory locations that are used to store values in a computer program.
So in computer programs, variables are a lot more than just names, they are pointers to memory locations used to store values.
The value can be anything at all — a number, a text, a list of numbers, etc. The variables just point to the value, so anytime the computer sees that variable, it replaces it with the value it is pointing to.
name = "John Doe
Example 1
What happens when a variable refers to another variable?
When a variable refers to another variable, the value of the later is copied into the former. For example:
Example 2
Two variables can refer to the same value. So once the value of one variable changes, it does not affect the other variable.