Repository navigation
Expand file tree
/
Copy path082-Update-Dictionary.py
More file actions
98 lines (80 loc) · 3.27 KB
/
Copy path082-Update-Dictionary.py
File metadata and controls
98 lines (80 loc) · 3.27 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
===============================================================================
File Name : 82-Update-Dictionary.py
Description : Update a Dictionary in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
A dictionary can be updated by changing the value of an existing key or by
using the update() method to add or modify multiple key-value pairs.
Syntax:
dictionary_name[key] = new_value
or
dictionary_name.update({key: value})
Example:
student["marks"] = 98
student.update({"course": "Java"})
"""
# -----------------------------------------------------------------------------
# Creating a student dictionary.
# -----------------------------------------------------------------------------
student = {
"id": 101,
"name": "Rahul",
"course": "Python",
"marks": 95
}
# -----------------------------------------------------------------------------
# Displaying the original dictionary.
# -----------------------------------------------------------------------------
print("Original Dictionary :", student)
# Output:
# Original Dictionary :
# {'id': 101, 'name': 'Rahul', 'course': 'Python', 'marks': 95}
print()
# -----------------------------------------------------------------------------
# Updating the value of the course key.
# -----------------------------------------------------------------------------
student["course"] = "Java"
# -----------------------------------------------------------------------------
# Displaying the updated dictionary.
# -----------------------------------------------------------------------------
print("After Updating Course :", student)
# Output:
# After Updating Course :
# {'id': 101, 'name': 'Rahul', 'course': 'Java', 'marks': 95}
print()
# -----------------------------------------------------------------------------
# Updating the value of the marks key.
# -----------------------------------------------------------------------------
student["marks"] = 98
# -----------------------------------------------------------------------------
# Displaying the updated dictionary.
# -----------------------------------------------------------------------------
print("After Updating Marks :", student)
# Output:
# After Updating Marks :
# {'id': 101, 'name': 'Rahul', 'course': 'Java', 'marks': 98}
print()
# -----------------------------------------------------------------------------
# Updating multiple key-value pairs using the update() method.
# -----------------------------------------------------------------------------
student.update({
"city": "Hyderabad",
"mobile": "9876543210"
})
# -----------------------------------------------------------------------------
# Displaying the final dictionary.
# -----------------------------------------------------------------------------
print("After update() Method :", student)
# Output:
# After update() Method :
# {'id': 101, 'name': 'Rahul', 'course': 'Java',
# 'marks': 98, 'city': 'Hyderabad', 'mobile': '9876543210'}
print()
# -----------------------------------------------------------------------------
# Displaying the total number of key-value pairs.
# -----------------------------------------------------------------------------
print("Total Key-Value Pairs :", len(student))
# Output:
# Total Key-Value Pairs : 6