VARIABLES IN PYTHON

Python Variables

While we write some programs in python, we are going to require storing some data somewhere in the program, for using in the program. For example, if you are going to write a python program, in which you are going to take the user’s name as user input, and then say hello to the user, with the name that the user has entered. This is a simple program, but we realize that we are required to store the value that the user enters somewhere, and we are going to use variables for storing our data.

In python, the variable can be considered as some name attached to some object. We can also say that the python variable is the symbolic name which is a reference to some object. Python is a dynamically typed programming language, which means that we do not have to specify the type of data that the variable is going to refer to.

So, now the question comes that how are we going to create variables. Well, if you are already familiar with some other programming language like C/C++ or Java(if you are not familiar, do not worry, since we are not going to go into that), then you might be aware that we have to declare the type of the data that we are going to store in the variable(which is not the restriction here in python!), and there was some particular format that we had to follow to create a variable.

But in python, you have to just give some name for the variable, and assign the value to it, and then you can use that variable.
So, to answer the question, Let’s create a variable, and store some integer data into it –

number = 10

As you can see, we named the variable a number and assigned the value as 10, and the variable is created. Note that the variable assignment is done using the simple assignment (=) symbol.

Here are some other examples for creating the variable –

email = ‘[email protected]

This time, the variable is being assigned with a string object. Well, the strings can be either in the single quotes (just like above) or in the double quotes as well, like in the below example –

company_name = “GyaniPandit”

So, the question might come what should we give then? Double or single quotes? Well, you can give anyone, but do not do something like double quote at one end and single quote at another end of the string, it will cause error. Later, we are going to have a look at the situations, when we need to use double quotes inside strings, or single quotes inside the strings, and at that time, you would definitely understand the use cases of using double quotes and single quotes for the string.

Soon, after this, we are going to study the concept of data types, which is nothing but defining the type of data that we are using. Since we require different types of data here, like the integer, floating point numbers, string, boolean (True or False), complex numbers, etc.., there are different data types for them as well.

Remember that in python, the variable is a reference to an object. This means that when we are creating a variable and assigning it an int type object, whatever the variable we have created, points to the int type object, and the value is into the object.

Now, when we are creating variables, we need to follow some rules while naming them. Those rules can be found in the Identifiers section ahead.

So, whenever we would require to store some kind of data in our program, we are going to create and use variables for that.

So, we would be making use of the variables, and gradually we would become familiar with them.

Let’s have a quick look at a python program, which demonstrates us about creating variables in the program.

As you can see in the above program, we have some variables, to which, we have assigned some data. Like in the first variable,

we have assigned an int type data, and in the second variable, we have assigned a string. In the third variable, we have assigned a boolean type data, and in the fourth variable, we have assigned a float type data.