1 list = ['larry', 'curly', 'moe']
2 list.append('shemp') ## append elem at end
3 list.insert(0, 'xxx') ## insert elem at index 0
4 list.extend(['yyy', 'zzz']) ## add list of elems at end
5 print list ## ['xxx', 'larry', 'curly', 'moe', 'shemp', 'yyy', 'zzz']
6 print list.index('curly') ## 2
7
8 list.remove('curly') ## search and remove that element
9 list.pop(1) ## removes and returns 'larry'
10 print list ## ['xxx', 'moe', 'shemp', 'yyy', 'zzz']
1EmptyList = []
2list = [1,2,3,4]
3#for appending list elements to emptylist
4for i in list:
5 EmptyList.append('i')
1class Student:
2 def __init__(self, name, age, grade):
3 self.name = name
4 self.age = age
5 self.grade = grade
6
7 def get_grade(self):
8 return self.grade
9
10class Course:
11 def __init__(self, name, max_student):
12 self.name = name
13 self.max = max_student
14 self.students = []
15
16 def add_student(self, student):
17 if len(self.students) < self.max_student:
18 self.students.append(student)
19 return True
20 return False
21
22 def get_average_grade(self):
23 value = 0
24 for students in self.students:
25 value += Student.get_grade()
26
27 return value / len(self.student)
28
29s1 = Student("Oshadha",20, 100)
30s2 = Student("Tom",20, 55)
31s3 = Student("Ann",20, 99)
32
33course = Course("Science", 2)
34course.add_student(s1)
35course.add_student(s2)
36print(Course.get_average_grade())
1 list = [1, 2, 3]
2 print list.append(4) ## NO, does not work, append() returns None
3 ## Correct pattern:
4 list.append(4)
5 print list ## [1, 2, 3, 4]