Question

In: Computer Science

Write a Python program that performs the following list operations. Part A Define a list called...

Write a Python program that performs the following list operations.

Part A

  1. Define a list called numList with elements.

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

  1. Display the followings:
    • All the numbers in numList
    • The number of elements in numList
    • The smallest number in numList
    • The largest number in numList
    • The sum of all the numbers in numList
    • The number of times 74 appears in numList

Part B

  1. Define an empty list called dogNames and display it.

  2. Add the following names to dogNames and display all the names in the dogNames list.

Max, Lola, Buddy, Coco, Teddy

  1. Display all the names in the dogNames list after each of the following updates.

    • Add the name ‘Benji’ at the end of the dogNames list.
    • Add the name ‘Chico’ at the beginning of the dogNames list (as the first element).
    • Add the name ‘Nala’ after the name ‘Coco’.
    • Add the name ‘Mimi’ before the name ‘Buddy’.
  2. Define another list called catNames with elements and display all the names in catNames list.

Kitty, Simba, Shadow, Coco, Pepper, Tiger

  1. 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.

  2. Display the followings using petNames list.

    • The total number of names in petNames.
    • The number of times the name ‘Coco’ appears in petNames.
    • The index of the pet name ‘Coco’ in petNames.
    • The first pet name that appears in chronological order based on Unicode. Hint:Use min function.
    • The last pet name that appears in chronological order based on Unicode. Hint: Use max function.
  3. Delete the last name in petNames list and display both the deleted name and the updated petName list.

  4. Delete the first name in petNames list and display both the deleted name and the updated petName list.

  5. Delete the first occurrence of the pet name ‘Coco’ using the remove() method and the deleted name and the updated petName list.

  6. In a comment, explain the reason to get None as the deleted name in your output for part 9.

Solutions

Expert Solution

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.


Related Solutions

Write a Program(code) in Python that performs a large number of floating-point operations and an equal...
Write a Program(code) in Python that performs a large number of floating-point operations and an equal number of integer operations and compares the time required. Please attach an output.
write a Python program (with comments) to do the following: Define a list list1 = [['Rose',...
write a Python program (with comments) to do the following: Define a list list1 = [['Rose', 'Daisy'], 7, 0, 5.5, [1, 22, 2.45, 3, 6, 5.8]] and print it. Print the length of list1 (i.e. number of items in list1) with a suitable message. Print the first character of the first string in the first item of list1 with a suitable message (HINT: The first item is the item with index 0). Print the sum of the last 4 numbers...
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and...
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and fill it with the names of various sandwiches. Make an empty list called finished_sandwiches. Loop through the list of sandwich orders and spring a message for each order such as "I am working on your tuna sandwich" As each sandwich is made, move it to the list of finished sandwiches. After all the sandwiches have been made, print a message listing each sandwich that...
1. Write a Python program that performs the following: 2. Defines an array of integers from...
1. Write a Python program that performs the following: 2. Defines an array of integers from 1 to 10. The numbers should be filled automatically without the need for user inputs 3. Find the sum of the numbers that are divisible by 3 (i.e., when a number is divided by 3, the remainder is zero) 4. Swap the positions of the maximum and minimum elements in the array. First, you need to find the maximum element as shown in the...
Write a program in python such that There exists a list of emails List Ls =...
Write a program in python such that There exists a list of emails List Ls = ['xyz@tz.ac.in','sachin.s@gail.com','avinash.paul@hotmail.com','rohan.d@xyz.ac.in',test@zx.ac.in.co'] Count the number of emails IDS which ends in "ac.in" Write proper program with proper function accepting the argument list of emails and function should print the number of email IDs and also the email IDS ending with ac.in output 2 xyz@tz.ac.in rohan.d@xyz.ac.in ================================= i am trying like this but getting confused len [True for x in Ls if x.endswith('.ac.in')] please write complete...
Write a Python program in a file called consonants.py, to solve the following problem using a...
Write a Python program in a file called consonants.py, to solve the following problem using a nested loop. For each input word, replace each consonant in the word with a question mark (?). Your program should print the original word and a count of the number of consonants replaced. Assume that the number of words to be processed is not known, hence a sentinel value (maybe "zzz") should be used. Sample input/output: Please enter a word or zzz to quit:...
for Python 3 Write a python program that Creates a list that has these values in...
for Python 3 Write a python program that Creates a list that has these values in this order, 'Python', 'JavaScript', and 'PHP' Define a displayMyClasses function that sorts the list and then displays each item on the list, one per line, in sorted order. Also, number the displayed list as shown in the "Running a Sample Program" shown below. Define a guessNext function that selects a random element from the list and returns it to the call of the function....
Use Python program. Make a list called groceries with the following values: "pineapple", "tangerine", and "peach"....
Use Python program. Make a list called groceries with the following values: "pineapple", "tangerine", and "peach". Define these two dictionaries in your program: stock = {     "pineapple": 6,     "peach": 0,     "tangerine": 32,     "pear": 15 } prices = {     "pineapple": 4,     "peach": 2,     "tangerine": 1.5,     "pear": 3 } Take one argument “item” as input from the user and returns the cost of that item. Assume the user will only input an item that...
Write a python program using the following requirements: Create a class called Sentence which has a...
Write a python program using the following requirements: Create a class called Sentence which has a constructor that takes a sentence string as input. The default value for the constructor should be an empty string The sentence must be a private attribute in the class contains the following class methods: get_all_words — Returns all the words in the sentence as a list get_word — Returns only the word at a particular index in the sentence Arguments: index set_word — Changes...
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called...
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called glossary. Have the glossary include a list of five (5) programing words you have learned in chapters 4-6. Use these words as keys to your dictionary. Find the definition of these words and use them as the values to the keys. Using a loop, print each word and its meaning as neatly formatted output. Create three dictionaries called person. Have each of the person...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT