Delete variable from python | Laptrinhcanban.com

HOME › >>

Delete variable from python

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
print(num)

del num

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
print(num)

# 10

del num
print(num)

The NameError returns:

Traceback (most recent call last):
File "Main.py", line 7, in <module>
print(num)
NameError: name 'num' is not defined

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
print(num)

>Traceback (most recent call last):
> File "Main.py", line 1, in <module>
> del num
>NameError: name 'num' is not defined

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

https://laptrinhcanban.com/en/python/nhap-mon-lap-trinh-python/bien-trong-python/xoa-bien-trong-python/

Thank you for reading and please Like & Share for other friends to learn.
">

HOME  › >>

  • Recent Posts
Profile
きよしです!笑

Author: Kiyoshi (Chis Thanh)

Kiyoshi was a former international student in Japan. After graduating from Toyama University in 2017, Kiyoshi is currently working at BrSE in Tokyo, Japan. Kiyoshi là một cựu du học sinh tại Nhật Bản. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.