-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection_in_Python.py
More file actions
488 lines (257 loc) · 11.5 KB
/
Collection_in_Python.py
File metadata and controls
488 lines (257 loc) · 11.5 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
"""
Collection is nothing but data types in python which allow us to store multiple value in single variable are known as Collection data types.
There are 4 collection data types in python:
1.) list []
2.) tuple ()
3.) set {}
4.) dictionary {"key" : "value"} """
""" 1) List [] The elements within a list can be mutable(changeable) or immutable(unchangeable). Lists themselves are mutable """
colors = ["orange", "green", "white", "blue"];
print(colors[0]);
print(colors[1]);
print(colors[2]);
print(colors[3]);
print("************************");
""" functions of list """
# 1.) len()
print("length of a list:", len(colors));
print("************************");
# 2.) for loop with range with list
for i in range(len(colors)):
print(colors[i]);
print("************************")
# 3.) for-each loop with list
for name in colors:
print(name);
print("************************");
# 4.) Updating the elements in the list
print("original list:", colors);
print("************************");
colors[2] = "grey";
print("updated list:", colors);
print("************************");
# 5.) Appending the elements to the end of the list
print("before appending list:", colors);
print("************************");
colors.append("olive");
print("after appending list:", colors);
print("************************");
# 5.) Inserting the elements at a perticular index
print("before inserting an element in list:", colors);
print("************************");
colors.insert(2, "yellow");
print("after inserting an element in list:", colors);
print("************************");
# 6.) Removing the elements at a perticular index
print("before removing an element from list:", colors);
print("************************");
colors.remove("yellow")
print("after removing an element from list:", colors);
print("************************");
# 7.) Removing the last element from the list
print("before poping last element from list:", colors);
print("************************");
colors.pop();
print("after poping last element from list:", colors);
print("************************");
# 8.) Removing the elements using their index using pop(index) and returns the removed element
print("before removing element by using pop(index) from list:", colors);
print("************************");
colors.pop(3);
print("after removing element by using pop(index) from list:", colors);
print("************************");
# 9.) Removing all the elements from the list using clear()
"""
print("before removing all elements by using clear() without deleting the list:", colors);
print("************************");
colors.clear();
print("after removing all elements by using clear() without deleting the list:", colors);
print("************************"); """
# 10.) Reversing the elements of the list using reverse()
print("before Reversing the elements of a list by using reverse():", colors);
print("************************");
colors.reverse();
print("after Reversing the elements of a list by using reverse():", colors);
print("************************");
# 11.) Sorting the elements of the list in alphabetical order using sort()
print("before Sorting the elements of a list by using sort():", colors);
print("************************");
colors.sort();
print("after Sorting the elements of a list by using sort():", colors);
print("************************");
# 12.) Nested list in python
a = [9,[6, 0, 6, 8], 5, 7]
print(a[1]);
print("************************");
print(a[1][3]);
print("************************");
# 13.) Forward and Backward index
num = [9, 1, 5, 7]
print(num[-2]) # backward index
print("************************");
# 14.) Slicing lists
# a) num[1:3] - index 1 to 3rd element
# b) num[:] - index 0 to last element (till length)
# c) num[:4] - index 0 to 4th element
print("before Slicing the list:", num);
print("************************");
print(num[1:3]); # index (included) : position (excluded) (given_num - 1 = index)
print("after Slicing the list:", num);
print("************************");
# 15.) Finding the number of times the element is repeated in the list using count()
digits = [9, 1, 5, 7, 6, 0, 6, 8, 9, 9, 5, 9, 9]
print("Occurenc of element 9 is:", digits.count(9))
print("************************");
# 16.) Finding the maximum and minimum elements in the list using max and min()
print("Maximum value from given list is:", max(digits))
print("Minimum value from given list is:", min(digits))
print("************************");
# 17.) Finding the sum of elements in the list using sum()
print("Sum of the elements of given list:", sum(digits))
print("************************");
# 18.) Finding the data type of the list using type()
print("Data type is:", type(digits))
print("************************");
# 19.) Deleting the list using delete list_name
print("before Deleting the list:", digits);
print("************************");
""" del digits
print("after Deleting the list by using del:", digits);
print("************************"); """
# 20.) Using in and not in operator with list to check whether an element is present in the list or not, returns true or false
print("Is element 5 present within the list:", 5 in num)
print("************************");
print("Is element 0 present within the list:", 0 in num)
# 21.) Concatenating two lists using + operator
""" print("after Concatenating num and digits two lists:", digits + num) """
print("************************");
# 22.) Extending the list
num.extend(digits)
print("after Extending the num list:", num)
print("************************");
# ************************************************************************************************************************************* #
""" 2) Tuple () "The elements within a tuple can also be mutable(changeable) if it contain elements, such as lists or dictionaries, those elements can be changed In this case, the list or dictionary inside the tuple can be modified, but the tuple itself cannot be changed (e.g., you cannot replace the list or dictionary with another element)."""
# diffrence b/w list and tuple
"""
1.) List elements can be updated hence it is called mutable(changeable) but Tuple elements can not be updated hence it is called immutable(unchangeable).
2.) In tuple we can not use append(), insert(), remove(), pop(), sort() and reverse() comands because tuple is immutable(unchangeable).
3.) If we create a list inside of a tuple only, list will be mutable
"""
# functions of Tuple ()
a = (9, 1, 5, 7)
print("Tuple is:", a)
print("************************");
print(type(a))
print("************************");
print("accessing the element using index:", a[2])
print("************************");
print("Length of tuple:", len(a))
print("************************");
# Typecast String into tuple
b = tuple("vinay")
print(b)
print("************************");
# """ Set - Collections {} """
""" List [] vs Tuple () vs Set {} """
"""
1.) List is changeable, Tuple is not changeable but Set is also changable. However, the elements within a set must be immutable(unchangeable).
2.) List has indexing, Tuple also has indexing but Set dosen't support indexing hence duplicates are ignored it will have unique values.
3.) List and Tuple's elements are stored in the same given order but in Set elements are stored in random order.
"""
# functions of Set {}
a = {9, 1, 5, 7}
print("Print the set:", a)
print("************************");
print("Data_Type of a:", type(a))
print("************************");
# length of set using len()
print("Find the length of a:", len(a))
print("************************");
# adding an element within set using add()
print("before Adding the value in set by using add():", a);
print("************************");
a.add(8)
print("after Adding the value in set by using add():", a);
print("************************");
# adding multiple elements within set using update()
print("before Adding the multiple values in set by using update():", a);
print("************************");
a.update([4,12])
print("after Adding the multiple values in set by using update():", a);
print("************************");
# removing an element from set using remove()
print("before Removing the element from set by using remove():", a);
print("************************");
a.remove(5)
print("after Removing the element from set by using remove():", a);
print("************************");
# discard an element from set by using discard(), if we pass an element which is not available within set discard 'function' will not give any error it will ignore but if we pass the same element with remove function it will give "KeyError" error hence we can say that discard is a smart element removing function.
""" a.remove(15) """
a.discard(15)
# randomly rmoving the value from set using pop()
print("before randomly Removing the vlaue from set by using pop():", a);
print("************************");
a.pop()
print("after randomly Removing the element from set by using pop():", a);
print("************************");
# rmoving all the values from set using clear()
print("before Removing all the elements from set by using clear():", a);
print("************************");
""" a.clear()
print("after Removing all the elements from set by using clear():", a);
print("************************"); """
# ******************************************************************************************* #
"""
empty_tuple = ()
# or
empty_tuple = tuple()
empty_set = set()
empty_dict = {}
# or
empty_dict = dict()
# or
empty_dict = {key: value for key, value in []}
"""
# deleting a set
print("before Deleting the set by using del 'keyword':", a);
print("************************");
""" del a
print("after Deleting the set by using del keyword:", a);
print("************************"); """
# for loop [for i in range()] with range() - is not possible with set
# we can use for each loop with set
for e in a:
print(e)
""" 'Set' can only nest Tuple it can not nest List and Set.
Sets can only contain immutable (unchangeable) elements. This is because sets rely on the immutability of their elements to maintain their uniqueness """
a= {9, (6, 0 ,6 ,8), 5, 1, 7}
# Converting a List to a Set
a = [9, 1, 5, 7] # list
b = set(a) # conversion from list to set
print("type of b:", type(b))
print("************************");
# Union
a = {9, 1, 5, 7}
b = {8, 5, 6, 1}
print("Union of two sets a and b:", a.union(b)); # Union will return a set of 6 elements because element 1 and 5 are twice it will concider them only once. either we can use union() or we can use or '|' operator.
print("************************");
print("Union of two sets a and b:", a | b);
print("************************");
# Intersaction
print("Intersaction of two sets a and b:", a.intersection(b)); # it will only give the commman values from both sets
# either we can use intersection() or we can use '&' operator
print("************************");
print("Intersaction of two sets a and b:", a & b);
print("************************");
# Difference
print("Difference of two sets a and b:", a.difference(b));
print("************************");
print("Difference of two sets a and b:", a - b);
print("************************"); # {9, 7}
# Symmetric difference
print("Symmetric_Difference of two sets a and b:", a.symmetric_difference(b));
print("************************");
print("Symmetric_Difference of two sets a and b:", a ^ b); # {6, 7, 8, 9}
print("************************");
# ******************************************************************************************* #
""" Dictionary - Collection """