[Solved] COMP1007 Lab 1- Create a Tuple
5.0
1 customer review
Digital download
Digital download
$25.00
Upload assignment
| list1 = [1, 3, 5, 7] print(list1)list2 = [Jack, John, Alice] print(list2)list3 = [1, Jack, 2, John] print(list3) #Generate a list from a range my_range = range(10, 20, 2) print(my_range) list4 = list(my_range) print(list4) #Generate a list from a tuple tuple1 = (John, Alice, Jack) list5 = list(tuple1) print(list5) #Is list5 equal to tuple1? print(list5 == tuple1) |
| Operation | Explanation |
| Indexing through [] | To access an item in the list |
| len(mylist) | Return the number of items in list mylist |
| mylist.append(object) | Add an object at the end of the list mylist |
| mylist.insert(index, object) | Add an object at the index location of the list mylist. The index begins with 0. |
| mylist.remove(object) | Remove the object from the list mylist. If multiple items in |
| the list have the same value, the first one will be removed. | |
| mylist.sort() | Sort the items in the list mylist. |
| mylist.reverse() | Rearrange the items of list mylist in reverse order. |
| list6 = [Jack, John, Alice] print(list6) print(list6[0]) print(list6[1]) print(len(list6)) list6.append(Jack) print(list6)list6.insert(2, Jack) print(list6) list6.remove(Jack) print(list6) list6.sort() print(list6) list6.reverse() print(list6) |
| family = {Jack:35, Linda:32, Alice:6} print(family) print(len(family)) family[David] = 68 print(family) print(len(family)) family[Alice] = 7 print(family) family.pop(David) print(family)stu = {17212345:[CHAN DAI MAN, 20000101, Male], 17216789:[WONG Maggie, 20000621, Female]} print(stu[17212345])#stu[17212345] is a list. So what is stu[17212345][0]? print(stu[17212345][0]) print(stu[17216789])#stu[17216789] is a list. So what is stu[17216789][2]? print(stu[17216789][2]) #add the Address information into the dictionary stu[17212345].append(KOWLOON TONG) print(stu[17212345])stu[17216789].append(SAI KONG) print(stu[17216789]) |