Nearby lessons
55 of 108Python - del and None
Introduction
Python provides the del keyword to delete variables.
Python also provides the special value None, which represents "no value".
Although both are related to object management, they work differently.
del Statement
del is a keyword in Python.
After using a variable, it is recommended to delete it if it is no longer required.
After deleting the variable, the corresponding object becomes eligible for Garbage Collection (if no other references exist).
Syntax
main.py
No output captured.
Example 1 - Delete a Variable
main.py
10
Example 2 - Accessing a Deleted Variable
After deleting a variable, it cannot be accessed.
If we try to access it, Python raises a NameError.
Program
main.py
NameError: name 'x' is not defined
Important Note
We can delete variables that point to immutable objects.
However, we cannot delete individual elements of an immutable object.
Example - Immutable Object
main.py
durga TypeError: 'str' object doesn't support item deletion
Why Does This Error Occur?
A string is an immutable object.
Immutable objects cannot be modified after creation.
Therefore, deleting an individual character from a string is not allowed.
Difference Between del and None
Both del and None are used while working with variables, but their behavior is different.
Case 1 - Using del
- The variable is removed.
- The variable cannot be accessed afterwards.
- It is called an Unbind Operation.
Example - Using del
main.py
NameError: name 's' is not defined
Case 2 - Assigning None
- The variable is not removed.
- The previous object becomes eligible for Garbage Collection (if no other references exist).
- The variable still exists.
- It is called a Rebind Operation.
Example - Assign None
main.py
None
Explanation
After assigning None, the variable still exists.
Its value becomes None.
Therefore, it can still be accessed.
Comparison Between del and None
| del | None |
|---|---|
| Removes the variable. | Does not remove the variable. |
| Variable cannot be accessed. | Variable can still be accessed. |
| Unbind operation. | Rebind operation. |
Raises NameError when accessed. |
Prints None. |
| Object becomes eligible for Garbage Collection. | Previous object becomes eligible for Garbage Collection. |
Real World Usage
The del keyword and None are commonly used in:
- Memory management
- Large applications
- Object cleanup
- Resetting variable values
- Garbage collection optimization
Important Notes
delis a Python keyword.delremoves the variable completely.- After using
del, the variable cannot be accessed. - Deleting an element from an immutable object (such as a string) is not allowed.
Nonerepresents the absence of a value.- Assigning
Nonedoes not remove the variable. - After assigning
None, the variable can still be accessed. delperforms an unbind operation, whereas assigningNoneperforms a rebind operation.
Quick Summary
| Topic | Description |
|---|---|
del |
Deletes a variable. |
Access after del |
Raises NameError. |
| Deleting an element from an immutable object | Raises TypeError. |
None |
Represents no value. |
Assigning None |
Variable remains available. |
| Garbage Collection | Previous object becomes eligible if no references exist. |