Python Variables

By | July 19, 2019

What is a Python Variable?

A Python variable is actually a named memory location to store some data used in a program. For example, suppose we have to write a simple addition of two numbers program in Python. Here, we will use three variables or three named memory cells for this purpose: we will use a variable ‘num1’ for storing first numer, ‘num2’ for storing the second number and  variable ‘sum’ to store addition of the two numbers.

num1 =10
num2 =20
sum = num1 + num2

We can change the value of a variable at any time while a program is running.
print(‘Sum=’,sum) # prints 30
num1=50 #changed the value of num1
num2=100 #changed the value of num2
sum = num1+num2 #value of sum will change
print(‘Sum=’,sum) # prints 150

What happens When we assign a new value to a variable

A variable can hold only one value in it. So if we assign a new value to a variable, the old value will be lost. For example:

num1=10  #num1 has 10

num1 = 200 #new value 200 will overwrite the old value 10

Terefore after execution of second assignment statement the num1 variable has 200.

A Python variable has 4 properties: name of variable, value of variable, type of variable and address of variable. The name is for identification. The value is the data to store in a variable. The type is decided on the basis of the value stored in a variable. Finally, a variable has an address in memory. We just use the name and value for a variable. Python manages type on the basis of value in a variable. Moreover, python manages the address with the help of memory management modules of the operating system.

We will use three variables as num1, num2 and sum. The variable num1 is for storing the first number. A variable num2 will store the second number. Finally a third variable ‘sum’ will store sum of two numbers

Python Variables - Graphical representation in memory-RAM by easycodebook

Python Variables – Graphical representation in memory-RAM by easycodebook

How to use Python Variables in a Program

In a Python program we just assign some value to a variable. The Python programming language will create automatically a variable for you with the name you used. It identifies the type of variable by the value you assigned to your variable.

Example: if we write in our Python program:


Python will create a variable with name ‘num1’ and type integer. Because num1 stores a whole number 10 which is integer value. Python uses integer data type for storing whole numbers.

We may use these variables as follows:

num1 =10
num2 =20
sum=num1 + num2
print('Sum of two numbers=',sum)

And the output of the program will be:
Sum of two numbers= 30

The Role of Variables in Python Programs

The variables play a are very important role in a program . Actually, varibles store the program’s data. A program will use input data, intermediate results and final results of data manipulation. All these data are processed with the help of variables.




2 thoughts on “Python Variables

  1. Hairstyles Cook

    It抯 actually a cool and useful piece of information. I am happy that you simply shared this useful information with us. Please keep us informed like this. Thank you for sharing.

  2. tiny url com

    Hello there! This is kind of off topic but I need some help from an established blog.
    Is it tough to set up your own blog? I’m not very techincal but I can figure things out pretty fast.
    I’m thinking about creating my own but I’m not sure where to begin. Do you have any tips or suggestions?
    With thanks


Leave a Reply

Your email address will not be published. Required fields are marked *