When it comes to learning a programming language, almost everyone starts their journey with variables. I know, some of you probably have been exhausted of studying them over and over again in every new language you learn and that it may seem banal to you. But understanding it a bit more will help you in the long run.
Boxes! That’s all they are. Whenever you ask a programmer about it, it’s most likely that they’ll tell you that “a variable can be thought of as a box that has something in it” P.S. it’s more than that, but the analogy helps to get started with it.
Well, theoretically, a variable can store anything be it numbers, words, sentences, tabular data, images, videos, etc though understanding the latter few is a bit tricky but not impossible. We’ll look at the basics of how they look in programming.
For the sake of this article we’ll use python which is a high-level programming language, which just means that it is human-friendly. So, let’s start with storing some numbers in the variables as any kind of data in existence can be boiled down to numbers.
There is also this whole domain of data types where we study what kind of data your variables can store.
Let’s say, we want to store number
6 in a variable. We do that by choosing a variable name (there are some exceptions in every programming language for naming your variables, be sure to look them up), in this case
a (you can name your variable anything, keeping in mind the exceptions that programming language has) and assigning it a value of
= is called an assignment operator in programming languages (well most of them at least). It is used to assign value to a variable.
a = 6
It is then obvious to wonder how do we imply “equality” in programming then. For that, we use
== two equal signs and this is called a relational operator. We’ll talk about it later sometime.
You can then manipulate this variable further more as this variable is not constant. In some programming languages, you can explicitly define a constant variable. A constant variable just means that its value cannot be changed anywhere else in the program rather when declaring it.
a = 6 + 1
b = 2
c = a + b
In the above code we have redeclared
a as sum of
1 on first line. Next we declared a new variable
b and stored
2 in it. Lastly, we declared
c and stored the sum of
b in it i.e. result of
7+2 will be stored in
c. On the last line the compatibility of variables matter. You can’t add a number and a word right? So we’ve to make sure the variables on both side of the operator store the same kind of things, in this case, a number.
Feel free to experiment with this. I know there’s much more to this but baby steps.
Till next time 😉