What does del mean in python?

青灯夜游
Release: 2020-07-23 14:41:38
Original
19027 people have browsed it

In python, del means "delete". You can delete an element in the list or the entire list. del deletes elements based on the index (the location of the element); del deletes variables, not data.

What does del mean in python?

del can delete the element at the specified position in the list; it is deleted based on the index (the position of the element).

The usage of del in python is quite special. Novices often misunderstand when learning. Understanding the usage of del can help to deeply understand the memory issues of python.

Python's del is different from C's free and C's delete.

Since python is all about references, and python has a GC mechanism, the del statement acts on variables, not data objects.

if __name__=='__main__':  
    a=1       # 对象 1 被 变量a引用,对象1的引用计数器为1  
    b=a       # 对象1 被变量b引用,对象1的引用计数器加1  
    c=a       #1对象1 被变量c引用,对象1的引用计数器加1  
    del a     #删除变量a,解除a对1的引用  
    del b     #删除变量b,解除b对1的引用  
    print(c)  #最终变量c仍然引用1
Copy after login

del deletes variables, not data.

if __name__=='__main__':  
    li=[1,2,3,4,5]  #列表本身不包含数据1,2,3,4,5,而是包含变量:li[0] li[1] li[2] li[3] li[4]   
    first=li[0]     #拷贝列表,也不会有数据对象的复制,而是创建新的变量引用  
    del li[0]  
    print(li)      #输出[2, 3, 4, 5]  
    print(first)   #输出 1
Copy after login

Recommended learning: Python video tutorial

The above is the detailed content of What does del mean in python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template