Posted at: https://laptrinhcanban.com/en
Instructions on how to delete variable from python. You will learn how to delete a variable that was previously declared using the del command in python after this lesson.
Delete variable from python
We can delete a variable that has been declared and prevent it from being used anymore by using the del command in python .
Syntax of deleting variables in python with del command in python is as follows:
del
variable
For example:
num = 10 |
After declaring the variable num, we used the command del to delete it. Once num
was removed , the variable return to the state has not been declared , so if we use this variable without declaring it, NameError errors will occur as follows:
num = 10 |
The NameError returns:
Traceback (most recent call last): |
When writing your program, if for some reason you want to delete variable from python, remember to use the del command .
And note that you must only be able to delete a variable that was declared previously, otherwise a NameError will occur:
del num |
Summary
Above, Kiyoshi explained how to delete variable from python with the del command in python already. To better understand the lesson content, practice rewriting today’s examples.
And let’s learn more about Python in the next lessons.
URL Link
HOME › python for beginners>>variables in python