-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.py
More file actions
63 lines (42 loc) · 1.38 KB
/
dictionary.py
File metadata and controls
63 lines (42 loc) · 1.38 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
'''
DICTIONARY
Dictionaries are used to store key-value pairs which are mutable, or changeable, and ordered.
'''
#empty dictionary
dict = {}
print("This is an empty Dictionary ", dict, end="\n\n")
#dictionary with elements
dict = {
1: 'Python', 'Two': 'Java', 3: 'Swift',
}
print("Elements of dictionary are \n", dict, end="\n\n")
''' Adding Elements '''
print("**********Adding Elements**********")
#changing element
dict[3] = 'C++'
print(f"Changed 3rd element \n {dict}", end="\n\n")
#adding key-value pair
dict['Fourth'] = 'Swift'
print("Added element \n", dict, end="\n\n")
''' Accessing Elements '''
print("**********Accessing Elements**********")
#access elements using keys
print("First element is ", dict[1], end="\n\n")
print("Fourth Element is ", dict.get('Fourth'), end="\n\n")
#get keys
print(f"Keys of the dictionary are \n{dict.keys()}", end="\n\n")
#get values
print(f"Values of the dictionary are \n{dict.values()}", end="\n\n")
#get key-value pairs
print(f"Dictionary is \n{dict.items()}", end="\n\n")
''' Deleting Elements '''
print("**********Deleting Elements**********")
#pop element
a = dict.pop('Fourth')
print(f"Deleted {a} \nNew dictionary {dict}", end="\n\n")
#pop the key-value pair
b = dict.popitem()
print(f"Popped {b} \nNew dictionary {dict}", end="\n\n")
#empty dictionary
dict.clear()
print(f"Cleared dictionary {dict}", end="\n\n")