From the previous sections, you must have got a basic idea about what a variable is. In short, it is similar to what we use in Maths but a lot more than that.
Basically, a variable is used to store a value. You can think of a variable as storage that has a name and stores some value.
Consider the following statement.
a = 10
Here, a is a variable that stores a value of 10.
Reassigning Values to Variables in Python
We can change the value of a variable anytime.
# declaring a variable count
count = 1
print(count)
# reassigning value to the variable count
count = "Hello World"
print(count)
In the above example, a variable count
is assigned a value of 1 and its value is printed. Then the variable is reassigned a value of “Hello World” making its value equal to “Hello World”.
Assigning Values to Multiple Variables in Python
Look at the following example in which values are assigned to the variables a
and b
.
a=1
b=2
print(a,b)
We already know how to do this. But we can also assign the values in the above example as shown below.
a, b = 1, 2
If multiple variables have to be assigned the same value, then we can do that in a single line as follows.
a = b = "Blue"
Cool, right?
Python Swapping
Swapping means interchanging the values of two variables. eg - if x is 10 and y is 5 then after swapping x will be 5 and y will be 10.
So, let's see how to do this.
x = 10
y = 5
x,y = y,x
print(x)
print(y)
And it's done. Yes, we know it is easy and that's why we love Python.
Deleting Variables in Python
We can delete Python variables using the keyword del
.
color = "Blue"
del color
In this example, del color
deletes the variable color
. After deleting, if you will try to access or print the variable color
, it will result in an error.
Memory Management in Python
In Python, when we use a variable, it is stored in the memory of the computer. For example, two variables having values 5 and 10 will be stored in two different memory locations by Python.
If a variable is assigned a particular value, then that variable points to the memory location of that value. Consider the following example.
a = 10
Here, the value 10 is stored in some memory location and the variable a points to the memory location where 10 is stored.
We can check the memory location of a value by using the id()
function.
a = 10
print(id(10)) # printing memory location of value 10
print(id(a)) # printing memory location of value assigned to variable a
id(10)
returns the memory location where the value 10 is stored and id(a)
returns the memory location of the value assigned to the variable a
. Both returned the value 9079296 which is the memory address where 10 is stored.
Look at another example.
a = 10
b = 20
print(id(a)) # printing memory location pointed by variable a
print(id(b)) # printing memory location pointed by variable b
From the output, we can see that the variables a
and b
point to the memory locations 9079296 and 9079616 respectively. This also means that the values 10 and 20 are stored in the memory locations 9079296 and 9079616 respectively.
We can also point two variables to the same memory location.
var1 = 10
var2 = var1
print(id(var1)) # printing memory location pointed by variable var1
print(id(var2)) # printing memory location pointed by variable var2
In this example, var2
is assigned the value of var1
and so both the variables point to the same memory address.
Now that you know what a variable is, let’s study about the different types of values that a variable can store in the next chapter.