Home  >  Article  >  Backend Development  >  Detailed explanation of the usage of del function in python

Detailed explanation of the usage of del function in python

hzc
hzcOriginal
2020-07-04 13:31:2038790browse

In python, you can use the "del()" function to delete one or several consecutive elements. This function is used to delete elements. Its syntax is "del $data", and its parameter $data represents the data to be deleted. .

Detailed explanation of the usage of del function in python

Usage:

-------del is used for list operations to delete one or several consecutive elements.

# Define a list

>>> a = [1,2,3,4]

>>> a

[1,2,3,4]

##>>> del a[0] # Delete the 0th element

>>> a

[2,3,4]

>>> del a[1:3] # Delete elements starting from the 1st element to the 3rd element.

>>> a

[2]#Delete elements including the head but not the tail

>>> del a #Delete the entire list

>>> a

Traceback (most recent call last):

File "", line 1, in

NameError: name 'a' is not defined

>>>

Recommended tutorial: "

Python Tutorial"

The above is the detailed content of Detailed explanation of the usage of del function in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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