GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CODESCRACKER
  • Python
  • Java
  • C++
  • JavaScript
  • SQL
  • Quiz
  • C
  • C#
  • HTML
  • CSS
  • PHP
  • Networking
  • Operating System

This view

  • Original page
  • New address

GeezerVison is showing codescracker.com — Python del Keyword

  • Python Basics
  • Python Tutorial
  • Python Applications
  • Python Versions
  • Python environment setup
  • Python Basic Syntax
  • Python end (end=)
  • Python sep (sep=)
  • Python Comments
  • Python Identifiers
  • Python Variables
  • Python Operators
  • Python Ternary Operator
  • Python Operator Precedence
  • Python Control and Decision
  • Python Decision Making
  • Python if elif else
  • Python Loops
  • Python for Loop
  • Python while Loop
  • Python break Statement
  • Python continue Statement
  • Python pass Statement
  • Python break vs. continue
  • Python pass vs. continue
  • Python Built-in Types
  • Python Data Types
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python frozenset
  • Python Dictionary
  • List vs. Tuple vs. Dict vs. Set
  • Python Numbers
  • Python Strings
  • Python bytes
  • Python bytearray
  • Python memoryview
  • Python Misc Topics
  • Python Functions
  • Python Variable Scope
  • Python Enumeration
  • Python import Statement
  • Python Modules
  • Python operator Module
  • Python os Module
  • Python Date and Time
  • Python Exception Handling
  • Python File Handling
  • Python Advanced
  • Python Classes and Objects
  • Python @classmethod Decorator
  • Python @staticmethod Decorator
  • Python Class vs. Static Method
  • Python @property Decorator
  • Python Keywords
  • Python Keywords
  • Python and
  • Python or
  • Python not
  • Python True
  • Python False
  • Python None
  • Python in
  • Python is
  • Python as
  • Python with
  • Python yield
  • Python return
  • Python del
  • Python from
  • Python lambda
  • Python assert
  • Python Built-in Functions
  • Python All Built-in Functions
  • Python print() Function
  • Python input() Function
  • Python int() Function
  • Python len() Function
  • Python range() Function
  • Python str() Function
  • Python ord() Function
  • Python chr() Function
  • Python read()
  • Python write()
  • Python open()
  • Python Examples
  • Python Examples

Python del Keyword

The del keyword in Python is used when we need to delete an object. For example:

class CodesCracker:
    Name = "William"
    Age = "20"

print("An object 'CodesCracker' is created.")
del CodesCracker
print("The object 'CodesCracker' is now deleted.")

a = 10
print("\nAn object 'a' is created.")
del a
print("The object 'a' is now deleted")

b = [12, 324, 54, 564]
print("\nAn object 'b' is created.")
del b
print("The object 'b' is now deleted")

c = "Python Programming"
print("\nAn object 'c' is created.")
del c
print("The object 'c' is now deleted")

The snapshot given below shows the output produced by above program, demonstrating the del keyword in Python:

python del keyword

Note - Everything in Python are objects such as variables, lists, tuples, parts of lists, etc.

The question is, what will happen, if we try to access an object that was deleted using the del keyword, in the same program. Let's find out, using the program given below:

x = "Python Programming"
print("The value of 'x' is:", x)

del x

print("\nThe value of 'x' is: ", x)

The output produced by this program, is shown in the snapshot given below:

python del keyword example

saying that the variable x is not defined. Because, using the del keyword, this variable is deleted before the last statement. Therefore an exception named NameError is raised.

Using the del keyword, we can also delete particular item from an iterable like list. For example:

x = [10, 45, 78, 90]
del x[1]
print(x)

Using del x[1], element at index number 1 will get deleted, that is 45. Therefore the output is:

[10, 78, 90]

Similarly, an attribute of a class can also be deleted using the del keyword, in this way:

class CodesCracker:
    Name = "William"
    Age = "20"

del CodesCracker.Name

Now the Name attribute of the class CodesCracker is deleted. The del keyword can also be used to delete multiple items from an iterable. For example:

x = [10, 45, 78, 90, 12, 23, 56, 67]
del x[1:5]
print(x)

In the code, x[1:5], 1 is included, whereas 5 is excluded. These two are the index numbers of list. Therefore, the output is:

[10, 23, 56, 67]

Python Online Test


« Previous Tutorial Next Tutorial »
Practice Quizzes

Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes.

All Quizzes SQL Quiz Python Quiz Java Quiz C++ Quiz
Popular Topics

Explore the most popular programming and computer science tutorials on CodesCracker.

Python Tutorial Java Tutorial C++ Tutorial SQL Tutorial JavaScript Tutorial HTML Tutorial CSS Tutorial Computer Networking
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.