Repository navigation
Expand file tree
/
Copy path088-Delete-Dictionary.py
More file actions
70 lines (57 loc) · 2.22 KB
/
Copy path088-Delete-Dictionary.py
File metadata and controls
70 lines (57 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
===============================================================================
File Name : 88-Delete-Dictionary.py
Description : Delete a Dictionary in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
The del keyword is used to delete an entire dictionary from memory. After
deleting the dictionary, it can no longer be accessed.
Syntax:
del dictionary_name
Example:
del student
"""
# -----------------------------------------------------------------------------
# Creating a student dictionary.
# -----------------------------------------------------------------------------
student = {
"id": 101,
"name": "Rahul",
"course": "Python",
"marks": 95,
"city": "Hyderabad"
}
# -----------------------------------------------------------------------------
# Displaying the original dictionary.
# -----------------------------------------------------------------------------
print("Original Dictionary :", student)
# Output:
# Original Dictionary :
# {'id': 101, 'name': 'Rahul', 'course': 'Python',
# 'marks': 95, 'city': 'Hyderabad'}
print()
# -----------------------------------------------------------------------------
# Displaying the total number of key-value pairs.
# -----------------------------------------------------------------------------
print("Total Key-Value Pairs :", len(student))
# Output:
# Total Key-Value Pairs : 5
print()
# -----------------------------------------------------------------------------
# Deleting the entire dictionary.
# -----------------------------------------------------------------------------
del student
# -----------------------------------------------------------------------------
# Displaying a confirmation message.
# -----------------------------------------------------------------------------
print("The dictionary has been deleted successfully.")
# Output:
# The dictionary has been deleted successfully.
print()
# -----------------------------------------------------------------------------
# Attempting to access the deleted dictionary.
# -----------------------------------------------------------------------------
print(student)
# Output:
# NameError: name 'student' is not defined