"del" Statement - Delete Statement

This section provides a quick introduction of 'del' (delete) statement, which is a simple statement that deletes the definitions of given targets and their associated object references.

What Is "del" - Delete Statement? A "del", or delete, statement is a simple statement that deletes the definitions of given targets and their associated object references.

Note that a delete statement will not delete the object referenced by the target. It is the job of the Python system to manage dead objects.

Delete statements have several forms:

1. Delete Variable - It deletes a single variable target using this syntax:

del variable_name

# example code
# define a variable with an assignment statement
>>> x = "apple"
>>> x
'apple'

# delete the variable
>>> del x
>>> x
NameError: name 'x' is not defined

2. Delete Attribute - It deletes an attribute of a target object using this syntax:

del target_object.attribute_name

# example code
>>> import sys

# define an attribute with an assignment statement
>>> sys.x = "apple"
>>> sys.x
'apple'

# delete the attribute
>>> del sys.x
>>> sys.x
AttributeError: module 'sys' has no attribute 'x'

3. Subscription Assignment - It assigns a single object to an item (indexed or named) of a target object using these syntaxes:

del target_object[index]
del target_object[key_name]

# example code
>>> fruits = list(["apple", "orange"])
>>> del fruits[0]
>>> fruits
['orange']

>>> prices = dict(apple=0.99, orange=1.19)
>>> del prices["apple"]
>>> prices
{'orange': 1.19}

4. Delete Slice - It deletes a slice of a target object using this syntax:

del target_object[low:high]

# example code
>>> feet = [10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> del feet[2:4]
>>> feet
[10, 20, 50, 60, 70, 80, 90]

5. Bulk Delete - It deletes each target in a given sequence from left to right using one of these syntaxes:

del  target_1, target_2, ...,
del (target_1, target_2, ...,)
del [target_1, target_2, ...,]

# example code

>>> fruits = ["apple", "banana", "cherry", "strawberry", "raspberry"]
>>> a, b, c, s, r = fruits
>>> del c, s

>>> a, b, c, s, r = fruits
>>> del (c, s)

>>> a, b, c, s, r = fruits
>>> del [c, s]

>>> del fruits

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

Statements - Execution Units

 What Is Statement

 "pass" Statement - Do Nothing Statement

 Expression Statement - One Expression Only

 "=" Statement - Assignment Statement

"del" Statement - Delete Statement

 "import" Statement to Load Modules

 "if" Statement for Conditional Execution

 "while" Statement for Execution Loop

 "for" Statement for Iterative Execution

 "try" Statement to Catch Execution

 "with" Statement for Context Manager

 "match" Statement for Pattern Match

 Function Statement and Function Call

 Iterators, Generators and List Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB