How to delete or clear variable from memory in Python?
Computer memory is very important not just for storing data, code and instructions but also for the system’s performance. Variables are the simplest and most basic storage units. You must be knowing that Python variables are dynamically typed, i.e, they are declared as and when they are assigned/used.
Sometimes, even though you assigned a variable with some value in the program, you may realize later that that variable is not necessary. In such a scenario, you will want to delete that variable both from your program as well as your memory.
In this tutorial, you will understand how to delete a variable from the memory space in Python.
The del keyword
It is used to simply delete any Python object. (variables,arrays,lists,dictionaries etc).
Syntax: del(element to be deleted)
You can look into the below-coded example to understand this better.
Example 1: Deleting a variable
a=10 print(a) del(a) print(a)
Leave a Reply