Repository navigation
Expand file tree
/
Copy path087-Clear-Dictionary.py
More file actions
79 lines (65 loc) · 2.64 KB
/
Copy path087-Clear-Dictionary.py
File metadata and controls
79 lines (65 loc) · 2.64 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
71
72
73
74
75
76
77
78
79
"""
===============================================================================
File Name : 87-Clear-Dictionary.py
Description : Clear a Dictionary in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
The clear() method is used to remove all key-value pairs from a dictionary.
After calling this method, the dictionary becomes empty, but the dictionary
object still exists.
Syntax:
dictionary_name.clear()
Example:
student.clear()
"""
# -----------------------------------------------------------------------------
# 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 before clearing.
# -----------------------------------------------------------------------------
print("Key-Value Pairs Before Clear :", len(student))
# Output:
# Key-Value Pairs Before Clear : 5
print()
# -----------------------------------------------------------------------------
# Removing all key-value pairs from the dictionary.
# -----------------------------------------------------------------------------
student.clear()
# -----------------------------------------------------------------------------
# Displaying the dictionary after clearing.
# -----------------------------------------------------------------------------
print("Dictionary After clear() :", student)
# Output:
# Dictionary After clear() : {}
print()
# -----------------------------------------------------------------------------
# Displaying the total number of key-value pairs after clearing.
# -----------------------------------------------------------------------------
print("Key-Value Pairs After Clear :", len(student))
# Output:
# Key-Value Pairs After Clear : 0
print()
# -----------------------------------------------------------------------------
# Displaying the type of the dictionary.
# -----------------------------------------------------------------------------
print("Type of Dictionary :", type(student))
# Output:
# Type of Dictionary : <class 'dict'>