Python is a high-level, interpreted programming language, and one of the most popular programming languages present in the software industry. One of the most important but often overlooked concepts in python is that of variables. Let us have a look at it in detail.
In python, variables are identifiers that are used to address a memory location and hold a value.
Unlike other languages, we do not need to specify the type of a variable and can directly assign values to it.
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory.
Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables. A variable is created the moment you first assign a value to it.
To better understand this concept, we shall follow a simple example of a piece of code that is used to assign value 1 to a variable x and then print it.
Let us look at the python code and the corresponding output for this.
# Creating a variable x and assigning a value of 1 to it
X = 1
# Printing the value of the variable
print(x)
Output:
1
Now say you want to create another variable y and assign the same value of 1 to it as well. A straightforward way to do this would be to similarly create and assign y. Let us look at the python code and the corresponding output for this.
# Creating a variable x and assigning a value of 1 to it
x = 1
# Printing the value of the variable
print(x)
# Creating a variable y and assigning a value of 1 to it
y = 1
# Printing the value of the variable
print(y)
Output
1
1
Another shorter way of creating multiple variables is to do something similar to chain assignment, using x=y=1. Let us look at the python code and the corresponding output for this.
# Creating a variables x and y and assigning a value of 1 to it
x = y = 1
# Printing the value of the variable
print(x)
print(y)
Output
1
1
Now say you want to assign a value of 1 to the variable x and a value of 2 to the variable y.
Apart from the straightforward method shown previously, another different method to do this in the same line is shown using simple commas.
On the left-hand side we have comma-separated variable names and on the right-hand side of the assignment operator = we have the comma separated values for corresponding variables.
Let us look at the python code and the corresponding output for this.
# Creating a variables x and y and assigning a value of 1 to it
x,y = 1,2
# Printing the value of the variable
print(x)
print(y)
Output
1
2
Variables can also be used for reassignment. Say you want to reassign the value of the variable y to the variable x after creating and assigning them as shown previously.
We simply use the assignment operator = in the following manner, x=y. Let us look at the python code and the corresponding output for this.
# Creating a variables x and y and assigning a value of 1 to it
x,y = 1,2
# Printing the value of the variable
print(x)
print(y)
# Reassigning x to have value of y
x = y
# Printing the updated value of xÂ
print(x)
Output
1
2
2
Since a variable is a basic unit of storage python uses the type inference concept to handle assigned values.
In python, we do not need to statically type them. We can reassign them to different values of different data types without any other changes.
For example, if a variable x is created and initially assigned with a numerical value of 1, we can easily reassign it to contain some character value, say a.
Let us look at the python code and the corresponding output for this.
# Creating a variables x and assigning a value of 1 to it
x = 1
# Printing the value of the variable
print(x)
# Reassigning x to contain the character value a
x = ‘a’
# Printing the value of the variable
print(x)
Output
1
a
Variables in python are of two types - local and global. Let us have a brief overview of them with examples.
When you declare a variable within a function, it is called a local variable. The scope of such variables is restricted to the function in which it was first declared. They are deleted as soon as the function finishes execution to release memory space. Let us look at an example to see how we can create and use local variables in Python.
def printx():
"""
Function that declares and prints the value of local variable x
"""
x = 2
print("Value of local variable x is",x)
printx()
Output
Value of local variable x is 2
Global variables in python are different from local variables in the way that they are defined. Global variables are defined outside a function and used within a function whereas local variables are defined and used inside the function. Let us look at an example to see how we can create and use global variables in Python.
# Declaring and defining the global variable x
x = 3
def printx():
"""
Function that prints the value of global variable x
"""
x = 3
print("Value of global variable x is",x)
printx()
Output
Value of global variable x is 3
In this topic, we have learned the use and advantages of variables in a Python program, following a running example of a simple assignment and printing program, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.