-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_collections.py
More file actions
68 lines (40 loc) · 1.77 KB
/
Copy path7_collections.py
File metadata and controls
68 lines (40 loc) · 1.77 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
# list - mutable, can contain duplicate elements, ordered
studentL = ['hemanth', 'kumar', 'rakesh', 'vijay', 'kalyan', 'raj', 'rahul']
total_students = len(studentL)
print("We have", total_students, "students")
studentL.sort()
print("The sorted list of students is", studentL)
studentL.append('yana')
print("a new student yana joined the class, now the list is:", studentL)
studentL.remove('kumar')
studentL.remove('vijay')
print("kumar and vijay left the class, now the list is:", studentL)
studentL.pop(3)
print("the 3rd student has left the class, now the list is:", studentL)
new_students = ['ganesh', 'durgesh', 'ramesh']
print("new students have arrived, they need to put in class. The list of new students is:", new_students)
studentL.extend(new_students)
print("Now the class is:", studentL)
studentL.sort()
print("Once again sorted list:", studentL)
studentL.reverse()
print("Reverse sorting:", studentL)
# tuple - immutable, can contain duplicate elements, ordred
studentT = ('vishal', 'sunil', 'raghu')
print("The student tuple:", studentT)
no_of_hemanths = studentT.count('hemanth')
print("Total number of hemanths is", no_of_hemanths)
studentL.extend(studentT)
print("Updated student list:", studentL)
# set - mutable, cannot contain duplicate elements, unordered
studentS = {'Babu', 'Patel', 'Kohli', 'Babu', 'himesh', 'Patel'}
print("student set:", studentS)
studentS.pop()
print("Now set looks like:", studentS)
studentS.discard('Babu') # discard does not throw error for missing item, remove does
print("new set situation:", studentS)
studentS.add('Science')
print("new set", studentS)
studentS.update(studentT)
print('updated set', studentS)
# dictionary -- keyvalue pair, advanced set