-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGeneral Python Notes
217 lines (213 loc) · 6.13 KB
/
General Python Notes
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> # functions of dictionary
>>> # clear
>>> d1 = {"1": APPLE, "2":Ball}
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
d1 = {"1": APPLE, "2":Ball}
NameError: name 'APPLE' is not defined
>>> d1 = {1: "Apple", 2: "Ball"}
>>> d1.copy()
{1: 'Apple', 2: 'Ball'}
>>> d2 = d1.copy()
>>> d2
{1: 'Apple', 2: 'Ball'}
>>> type(d2)
<class 'dict'>
>>> d2.clear()
>>> d2
{}
>>> # 1.clear 2. copy 3. fromkeys = (it creates s new dict. from an iterable & it will use elements from iterable as Keys)
>>> # and for value it will use default values
>>> # example of fromkeys()
>>> a = {1:100, 2:200}
>>> b = (5,6,7) #tuple
>>> # tuple & list are iterables
>>> a.fromkeys(b)
{5: None, 6: None, 7: None}
>>> help(a.fromkeys(b))
Help on dict object:
class dict(object)
| dict() -> new empty dictionary
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs
| dict(iterable) -> new dictionary initialized as if via:
| d = {}
| for k, v in iterable:
| d[k] = v
| dict(**kwargs) -> new dictionary initialized with the name=value pairs
| in the keyword argument list. For example: dict(one=1, two=2)
|
| Methods defined here:
|
| __contains__(self, key, /)
| True if the dictionary has the specified key, else False.
|
| __delitem__(self, key, /)
| Delete self[key].
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __gt__(self, value, /)
| Return self>value.
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __repr__(self, /)
| Return repr(self).
|
| __reversed__(self, /)
| Return a reverse iterator over the dict keys.
|
| __setitem__(self, key, value, /)
| Set self[key] to value.
|
| __sizeof__(...)
| D.__sizeof__() -> size of D in memory, in bytes
|
| clear(...)
| D.clear() -> None. Remove all items from D.
|
| copy(...)
| D.copy() -> a shallow copy of D
|
| get(self, key, default=None, /)
| Return the value for key if key is in the dictionary, else default.
|
| items(...)
| D.items() -> a set-like object providing a view on D's items
|
| keys(...)
| D.keys() -> a set-like object providing a view on D's keys
|
| pop(...)
| D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
| If key is not found, d is returned if given, otherwise KeyError is raised
|
| popitem(self, /)
| Remove and return a (key, value) pair as a 2-tuple.
|
| Pairs are returned in LIFO (last-in, first-out) order.
| Raises KeyError if the dict is empty.
|
| setdefault(self, key, default=None, /)
| Insert key with a value of default if key is not in the dictionary.
|
| Return the value for key if key is in the dictionary, else default.
|
| update(...)
| D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
| If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
| If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
| In either case, this is followed by: for k in F: D[k] = F[k]
|
| values(...)
| D.values() -> an object providing a view on D's values
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| fromkeys(iterable, value=None, /) from builtins.type
| Create a new dictionary with keys from iterable and values set to value.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
>>> #a is used tp invoke the function ONLY
>>> dict.fromkeys(b)
{5: None, 6: None, 7: None}
>>> dict.fromkeys(a)
{1: None, 2: None}
>>> dict.fromkeys(b)
{5: None, 6: None, 7: None}
>>> dict
<class 'dict'>
>>> dict.fromkeys(b, 22)
{5: 22, 6: 22, 7: 22}
>>> dict.fromkeys(b, 22, 23)
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
dict.fromkeys(b, 22, 23)
TypeError: fromkeys expected at most 2 arguments, got 3
>>> # 4. get() function
>>> # it is used to acess values of dict.
>>> a.get(2)
200
>>> a.get(1)
100
>>> # dict has no support for INDEXES
>>> # 5. items() function
>>> # it makes a dict. iterable
>>> a.items()
dict_items([(1, 100), (2, 200)])
>>> # example for items()
>>> for i,j in a.items():
print(i, j)
1 100
2 200
>>> # keys() ==> it return a list of keys from dict
>>> a.keys()
dict_keys([1, 2])
>>> b.keys()
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
b.keys()
AttributeError: 'tuple' object has no attribute 'keys'
>>> # values() fun. ==> it returns list of values from dict
>>> a.values()
dict_values([100, 200])
>>> # pop and popitem
>>> a.pop()
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
a.pop()
TypeError: pop expected at least 1 argument, got 0
>>> a.pop(1)
100
>>> a
{2: 200}
>>> a.popitem
<built-in method popitem of dict object at 0x0000017C150CD480>
>>> # setdefault
>>> it extends the existing dict
SyntaxError: invalid syntax
>>> # update fun.
>>> # it adds new itemto a dict / it can update new value to pre existing key
>>> a.update({3:300})
>>> a
{2: 200, 3: 300}
>>>