The first element that you need to know about programming is the concept of variables. A variable is basically a space in your computer’s memory to save data during run time (i.e. while the program is running). This is similar to a human that is thinking something. The fact that he is keeping the thought in the back of his mind is comparable to the program storing data in a variable.
For making the concept a little bit more visual, you can think of a variable as something like a bucket. Inside this bucket, you can store data. The question is not, what is this data? And here comes the tricky part. In a bucket, you can fill whatever you want (even if it does not really fit inside and sticks a little bit out of the bucket). With a variable, you can not do the same as in programming languages like C or C++, every variable is made to store a specific type of data like numbers or words. (Python is a little bit less strict in this regards, but you should still know about this.)
This means that in C, if you want to store a number inside a variable, this variable needs to be made for that number type. You can’t simply store a word inside that variable. Also, it is not possible to store a number inside that variable if the number is too big. You could compare this like there were buckets especially made for numbers. Let’s say these buckets have a square shape. A square shaped number will fit inside the bucket, but a round shaped word will not fit inside as the bucket is not made for round shaped objects.
One thing that you also should notice is that the variable must exist before you store data inside of it. Similarly, you first need to have a bucket to place something inside the bucket. This is usually called, the variable is initialized and this is also where you define what type of data it is allowed to store.
The ‘variable for each type’ thing holds true for most languages out there, but Python is a little different. Remember what the programming languages guide said about Python? Python is doing some things for you, in this case, it will allow to fit every data type into every variable no matter what. It just doesn’t care about these rules as this is all happening in behind the scenes. Also, Python does not care about the initialization thing either. (What a rebel.)
An important thing about variables: each variable has a name. This name must be unique inside the scope of that program as otherwise, the computer might confuse the variables if they are all called the same name. The syntax is generally similar to the following:
variable_name = variable_value _variable = _value
Variables are often named with the words all lower case and separated by an underscore. The name can be anything you like but there is one rule: it must start with either a letter or an underscore. This means it can’t start with a number or a special character in front.
You can also assign the value of a variable to another variable.
variable1 = something variable2 = variable1
In this example, the variable called
variable1 will have the value
something and the
variable2 will have the same value as