Variables are containers used to store data and there are different types of data that can be stored: this section focuses on the Numeric data types while the section below describes Boolean data types. These types of variable are good to gain some familiarity with this programming language. In Python a variable is created by just assigning a name, which is the name of the variable, to a specific value. This is different from some programming languages like C++ or Java where you also need to define the type of variable that you are creating. Here, for example as shown in the code below we can create a variable "v" and assign the value 1 to it by using the "equal" sign and then use the print statement and when the code is executed the value of the variable will appear in the console. If you change the value of the variable and run it again then you see the new value.
v=1 print(v)
Almost any name can be used for the variables but there are a few rules. The most important are that the variable should start with letters or underscore and that they are case-sensitive. So, for example if now I add a new variable "V" and assign 5 to it when I print "v" and "V" they are seen as two different entities.
v=1 V=5 print(v) print(V)
It is good however to use variable names that are meaningful and give some context instead of simply keep creating variables like a, b, c and so on because by adding meaning it is possible to improve the readibility of the code. Here we are simply using these variable names because these are examples. A quick digression, to add comments in the script which you do not want to be considered as part of the code simply use the "#". For example "#This is a comment". You can also use this to hide some coding lines, so that when you run the script they are ignored.
#This is a comment not considered as part of the code v=1 V=5 print(v) print(V)
The Numeric data type is very common and important given that it is related to numbers. Two main types of numbers that are typically used are integers and floats. The integers, are the whole positive and negative numbers like -10, 0, 25 and so on. While the floats contain decimals like -5.8 or 2.47 and so on. Let's consider two variables $a1=8$ and $a2=2.5$. It is possible to perform some math operations on these variables such as: addition, subtraction, multiplication, division, exponentiation and modulus (i.e. returns the remainder of the division).
a1=8 a2=2.5 #Addition, the result is 10.5 print(a1+a2) #Subtraction, the result is 5.5 print(a1-a2) #Multiplication, the result is 20 print(a1*a2) #Division, the result is 3.2 print(a1/a2) #Exponentiation, the result is approximatly 181.0193.... print(a1**a2) #Modulus, the result is 0.5 print(a1%a2)
It is posible to increase or decrease the value of a variable by setting the variable equal to itself and then perform some math operation. Such as: $a1=a1+5$ or $a1=a1-2$ and so on. In Python there are also short forms to write this, such as: $a1+=5$ or $a1-=2$ (Essentially, use the math operator before the "equal" sign).
a1=8 a1=a1+5 print(a1) a1=a1-2 print(a1) a1+=5 print(a1) a1-=2 print(a1)
Also, it is possible to use parenthesis like in math to establish the order of the operations. For example, $a3=4+2*(3+5)$ will start with the sum within the parentheses and then do the multiplication and finally sum the result with the number 4 and we obtain 20. This overall gives an intro on the possible operations that are typically performed on Numeric variables, the next section will consider Boolean variables.
Other important operations are related to comparisons between variables. The result of a comparison can be True or False and this can be stored in a variable which then becomes a Boolean variable. For example: $a1==a2$ checks if the a1 contains the same value of a2. Given that it is not the case (the value of a1 previously used is different from a2), the result is False. We can write $b=(a1==a2)$ and now the variable b is a Boolean which contains the state "False".
a1=8 a2=5 print(a1==a2) b=(a1==a2) print(b)
Other comparison operators are: $a1>a2$, $a1 < a2$, $a1>=a2$, $a1 <= a2$ and then there is the $a1!=a2$ which checks if a1 is different from a2 (given that they have different values this is "True").
#Comparison operators a1=8 a2=5 print(a1==a2) # This is False print(a1>a2) # This is True print(a1>=a2) # This is True print(a1<a2) # This is False print(a1<=a2) # This is False print(a1!=a2) # This is True
So, the Boolean variables store if something is True or False and these states can be used to make a program behave in specific ways. Take some time to try the operations between variables described in this section using the console below given that they can be found in many occasions.
Below you can also see the video of my official Youtube Channel which visually shows these operations in Spyder.