What are variables?
Variables can be imagined as boxes to store stuff in. In actuality variables take up memory space to store the given data. They are required in almost every program in python for calculation, comparison and much much more.
Variables can be of various data types including int, string, lists, tuples etc. More on that later.
How to declare and use Variables?
Unlike other conventional programming languages, declaring a variable is far more simpler in python. All you have to do is think of a name for the variable and assign it to a value. You can use any name you want but just make sure to exclude any special characters.
Here’s an example.
#A variable called name is created and assigned the value of "Sam" name = "Sam" #The variable name is printed out print (name)
In the example above, we declared a variable called name in the first line and we assigned a value “Sam” to it with the assignment operator ( = ).
In the second line we called the print() method to print the value stored in the variable name.
What are Data Types?
Depending on the data that you assign to a variable, the data type of the variable changes. This feature is exclusive to python and the same thing does not work on C, C++, Java, C# where you have to declare the data type of the variable along with the value assigned.
Data Types can be classified as follows:
Numerical Data Type
Numerical data types can be any numbers or integers as values that are stored in a variable.
The most common numerical data type is integer. Here’s an example to declare the same.
#A variable called a is created and assigned the value of 10 a = 10 #Another variable named b is created and assigned the value of 20 b = 20 #The numbers are added and printed print (a+b)
As you can see, in the first line we created a variable called a and assigned it a value of 10. We also created another variable b and assigned it a value of 20. In the last line we called the print() method and added the variables up with the addition operator ( + ).
Any number with a decimal point is stored as a float in a variable. Here’s an example.
# A variable called num is created to store the float value. num = 11.55 # Printing out the value along with the type of variable print("The variable has a type of ", type(num), " with value ", num)
The variable has a type of <class 'float'> with value 11.55
In the example, we created a variable called num with value 11.55. All we actually have to do to declare a float variable is to simply assign the value to the variable (which is same for declaring variables with any data type).
Then we printed the value out along with the data type with the type() with our variable, num as an argument.
String Data Type
Strings are a collection or sequence of characters. Strings are usually mentioned with single quotes or double quotes. Any word or combination of letters can be considered as string.
Here’s an example:
name = "Derek" last = "Brown" print (name + " " + last)
In the example we created two variables called name and last and assigned their respective values. In the print statement we did something known as concatenation which combines the two strings and prints them out in a single line.