What is a variable?

  • A variable in simple words means an element, feature, or factor that is liable to vary or change.
  • They are memory location which is reserved to store values. When you create a variable, in memory some space is reserved. In this reserved space the value of the variable is stored.
  • Each value stored in a variable has a specific data type. The allocation of memory is done for the data type of the value. The interpreter allocates memory and decides what can be stored in the reserved space in memory.

Now let us see how to use variables in python.


How to assign a value to a variable?

  • In Python, variables need not be declared or defined in advance
  • To create a variable, you just assign it a value and then start using it.
  • The assignment is done with a single equals sign (=).

Let us understand this with an example:

Here in the above example, I have assigned a value ‘5’ to a variable ‘a’ and ‘4’ to another variable ‘A’.

NOTE: Here we can observe that the value is given and printed for variable ‘a’ and ‘A’ is different. This is due to that the Python is a case sensitive language where the capitals(Upper cases) and the small letters(lower cases) are treated differently. Their values are also considered different.  

Now to check whether the assigned value is used by the variable, I have used a print function to print the value of the variable.


How do variables work in Python?

To understand this concept let us assign a=7, b=a, a=3.

Here you observe that ‘a’ is assigned a value twice.

Does the question arise that what value of ‘a’ would be printed at the end?

Let us check this out.

The value of ‘a’ printed is ‘3’.

How did this happen?

This can be understood when we learn how the variable values are stored.


Given the value of a=7.

Here we have assigned a=7.


Given b=a.

That is, the value of b becomes equal to 7.(b=7)  #from case 1.


Value of a=3.

Here the pointer that was earlier pointing at 7 will move and now point to 3.

That is, now the pointer of a is at 3.  


This is now clear that the change in the value of a single variable leads to a change in the pointer.

Thus the value changes from ‘7’ to ‘3’.

Now on printing the value of ‘a’, the value assigned is ‘3’.  


How to name a variable?

Variables in Python can be named either using an alphabet(upper case/ lower case) or underscore followed by an alphabet.

NOTE: Using any special symbols or numerical value leads to error in python.  


How to determine the data type of the value used?

This can be done by using type( ) function.   

NOTE: Python supports integer(int), float(float), Boolean(bool), and string(str) data types where integer stores integer type value, float stores decimal type, Boolean stores data with one of two built-in values True or False. Notice that ‘T’ and ‘F’ are capital. true and false are not valid Booleans and Python will throw an error for them. String stores the character values in Python. Python doesn’t have any data type for the character, they are all treated as a string.

Let us use type() function to determine the data types:

Similarly, we can use Boolean and String values and identify them using type() function.

In this blog, we have covered the basics of variables assignment in Python. I hope you have understood.  

See u soon with another blog.

Stay tuned!!!!

Categories: Python

Tags: , , , ,

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: