In this tutorial, we shall be learning about what variables are used in Python, how are they used, their initialization and a lot more!
The types of variables are nearly same in all programming languages. But if you are not familiar with any other language or programming, then we will mention the types of the variables that are available.
Let us look at what all variable types are there which we can use.
1) Numbers: this type includes the integer type, float type, long type and complex numbers. In float type, there are decimal numbers, in integer as the name suggests are integer numbers, long is used to increase the range of these number types that is, float and integer. Complex numbers consist of combination of real numbers and imaginary numbers.
We can simply assign the value to any variable with the help of the assignment operator (=).
Let us look how.
a = 10
Here we have assigned a value (10) to the variable a.
We can also assign a single value to multiple variables.
s = g = h = 34
Here, we are assigning the value 34 to the variables s, g, h.
2) Strings which includes characters as well as special characters.
Let us look at some examples.
s = “Python Programming”
There are a few operations that we can perform as an initial learning for strings.
Let us look at some of them.
Here, we can see that we have initialized the string as “Python Programming” and are performing some operations on it.
First, we are just printing s which gives “Python Programming” as output.
Then we are printing the third letter of the string that gives us “h” as output. This is because the string is stored by zero as the starting index.
In the next print statement, we are printing the string starting from the third position till the tenth position. So, it prints “hon Pro” as a space is also counted.
In the last print statement, we are concatenating the string that is, adding a string to the present string. So, we get the output as “Python Programming is the best”
3) Lists is one of the most versatile features that is provided by python. It is like arrays in C++, the only difference between them is that that lists can hold multiple types values, but arrays can only hold a single type value of value.
Let us understand with the help of an example.
Here, we can see creating two lists that are a and b. both are having elements of different types like integer, character, decimal and performing operations that are similar to those which we performed on strings. So, the concepts remain same, but the datatype changed.
4) Tuples are like lists; they also store multiple type of data. They are enclosed within brackets “()”, while list is enclosed within square brackets “”. The only thing with tuple is that that we can not update the values in it but in lists we can.
Let us look at an example and try to understand the concepts of tuples.
What if we want to change the value of the second element in t that is, changing the value of 181 to some other value. Let us see what happens.
We got this error when we tried to change the value in a tuple.
5) Dictionary are like hash tables which works like associative arrays which consists of key-value pairs. The key values can be of any types, and values can be any arbitrary object of python.
That is, it for this tutorial, I hope you learned something from it. See you in the next one.