-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_practice_2.py
More file actions
130 lines (100 loc) · 1.84 KB
/
python_practice_2.py
File metadata and controls
130 lines (100 loc) · 1.84 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#Dictionaries:
dictionary={
'a':[1,2,3],
'b':'hello',
'x':True
}
my_list=[{
'a':[1,2,3],
'b':'hello',
'x':True},
{
'a':[4,5,6],
'b':'hello',
'c':True
}]
print(my_list[0]['a'][2])
print(dictionary['a'][1])
#A dictionary keys always has to be immutable.
user={
'basket':[1,2,3],
'greet':'hello',
'age':20
}
print(user.get('age',55)) #If its already age is assigned to a different value then it will print the previous value which is assigned to it.
dict_1={
'123':[1,2,3],
'123':'hello'
}
print(dict_1['123'])
#Dictionary Methods: I
user_1={
'basket':[1,2,3],
'greet':'hello'
}
print(user.get('year'))
#Dictionary Methods: II
user_2=dict(name='keshav')
print(user_2)
# .keys() Method:
user={
'basket':[1,2,3],
'greet':'hello',
'age':20
}
print('age' in user.keys())
print('you' in user.keys())
# .values() Method:
user={
'basket':[1,2,3],
'greet':'hello',
'age':20
}
print('hello' in user.values())
# .items() Method:
user={
'basket':[1,2,3],
'greet':'hello',
'age':20
}
print(user.items())
# .clear() Method:
print(user.clear())
print(user)
# .copy() Method:
user_3={
'basket':[1,2,3],
'greet':'hello',
'age':20
}
user_4=user_3.copy()
print(user_3.clear())
print(user_4)
# .pop()/ .popitem() Method:
'''
.popitem(): Removes the last items from the dictionary.
.pop(): Removes the values of keys entered in .pop().
'''
user_5={
'basket':[1,2,3],
'greet':'hello',
'age':20
}
print(user_5.pop('age')) # Gives the values which is poped.
print(user_5)
print(user_5.popitem())
# .update() Method:
user_6={
'basket':[1,2,3],
'greet':'hello',
'age':20
}
print(user_6.update({'age':50}))
print(user_6)
user_7={
'basket':[1,2,3],
'greet':'hello',
'age':20
}
print(user_7.update({'ages': 55}))
print(user_7)