In: Computer Science
Write a Python program that performs the following list operations.
Part A
84, 94, 27, 74, 19, 90, 16, 21, 56, 50, 77, 59, 41, 63, 18, 26, 80, 74, 57, 30, 40, 93, 70, 28, 14, 11, 43,65, 91, 83, 22, 53, 74, 44, 73, 55, 47, 74, 81
Part B
Define an empty list called dogNames and display it.
Add the following names to dogNames and display all the names in the dogNames list.
Max, Lola, Buddy, Coco, Teddy
Display all the names in the dogNames list after each of the following updates.
Define another list called catNames with elements and display all the names in catNames list.
Kitty, Simba, Shadow, Coco, Pepper, Tiger
Create a new list called petNames by joining the dogNames and catName list. Note that in petNames list, all the dog names come before the cat names. Display all the names in petNames list.
Display the followings using petNames list.
Delete the last name in petNames list and display both the deleted name and the updated petName list.
Delete the first name in petNames list and display both the deleted name and the updated petName list.
Delete the first occurrence of the pet name ‘Coco’ using the remove() method and the deleted name and the updated petName list.
In a comment, explain the reason to get None as the deleted name in your output for part 9.
Part A:
numList=[84, 94, 27, 74, 19, 90, 16, 21, 56, 50, 77, 59, 41, 63,
18, 26, 80, 74, 57, 30, 40, 93, 70, 28, 14, 11, 43,65, 91, 83, 22,
53, 74, 44, 73, 55, 47, 74, 81]
print("List is:",numList)
print("Numer of elements:",len(numList))
print("smallest number is:",min(numList))
print("Largest number is:",max(numList))
print("Sum of all elements is:",sum(numList))
print("Number of times 74 appears is:",numList.count(74))
Part B:
dogNames=[]
print("List is:",dogNames)
dogNames.append("Max")
dogNames.append("Lola")
dogNames.append("Buddy")
dogNames.append("Coco")
dogNames.append("Teddy")
print("List is:",dogNames)
dogNames.append("Benji")
dogNames.insert(0,"Chico")
dogNames.insert(dogNames.index("Coco")+1,"Nala")
dogNames.insert(dogNames.index("Buddy")-1,"Mimi")
print("List is:",dogNames)
catNames=[]
catNames.append("Kitty")
catNames.append("Simba")
catNames.append("Shadow")
catNames.append("Coco")
catNames.append("Pepper")
catNames.append("Tiger")
print("List is:",catNames)
petNames=[]
petNames=dogNames+catNames
print("List is:",petNames)
print("Total number of Names is:",len(petNames))
print("Number of times Coco appears is
:",petNames.count("Coco"))
print("Index of pet Coco is:",petNames.index("Coco"))
print("The first pet that appears in chronological order
is:",min(petNames))
print("The last pet that appears in chronological order
is:",max(petNames))
print("The deleted pet name is:",petNames[-1])
petNames.remove(petNames[-1])
print("List is:",petNames)
print("The deleted pet name is:",petNames[0])
petNames.remove(petNames[0])
print("List is:",petNames)
petNames.remove("Coco")
print("List is:",petNames)
Screenshots:
The screenshots are attached below for reference.
Please follow them for output.
Please upvote my answer. Thank you.
Note:
Please see that as per the guidelines, we are allowed to answer maximum upto one question posted in a single question.
In case of multiple choice type questions, upto 4 questions cn
be answered.