Python Delete Variable | How to Delete a Variable in Python?

Here, we have discussed the tutorial for how to delete a variable in python. Python variable are defined as the name given to memory location...

Hey guys, welcome to a new interesting article related to python programming language. If you are searching tutorial for how to delete a variable in python, then you have come to the right palace. 

how to delete a variable in python | python delete variable

In this article we will going to discuss about how to delate a variable in python programming language. But at first let’s learn about python and python variable.

What is Python?

Python programming language is an open source, general purpose, easy to use programming language which also provides the feature of embeddability and extensibility. 

What is Python variable?

Python variable are defined as the name given to memory location. In other words, a reserved location of memory for storing data and values is known as python variable. 

How to Delete a Variable in Python?

In order to delete a variable in python programming language we can use the del keyword. We can use the following syntax to delete a variable in python programming language;

del <variable_name>  

For example:

x = "Welcome to Answersjet"
print(x)  
# Deleting a variable  
del x  
print(x)  

Output

Welcome to Answersjet

Traceback (most recent call last):

  File "main.py", line 5, in <module>

    print(x)  

NameError: name 'x' is not defined

In the above example, we have created a variable x and then assigned a value to the variable. Then we have deleted the variable x and then printed it, and as a result we got an error that says “variable x is not defined” which means that in future there will be no use of variable x.

Conclusion

Above we have discussed the tutorial for how to delete a variable in python. Python variable are defined as the name given to memory location. In other words, a reserved location of memory for storing data and values is known as python variable. By following the above method anyone can easily able to delete a variable in python programming language.