Question: For questions vital: File: usePythonList.py This program exercises Python built-in lists. # # Replace with your name. # Replace any comments with

For questions vital:

File: usePythonList.py

This program exercises Python built-in lists. """

#

# Replace with your name. # Replace any "" comments with your own code statement(s) # to accomplish the specified task. # DO NOT CHANGE ANY OTHER CODE.

# Here is your starting list: myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

# Print the list: print("myList:") print(myList)

# Part 1: # Print the items at indexes 0, 6, and 11: print() print("Items at indexes 0, 6, and 11:") #

# Part 2: # Use the len() function to print the number of items in the list: print() print("Length of list:") #

# Part 3: # Use the append() method to add the number 7 to the end of the list: print() print("Using append() to add the number 7 to the end of the list:") # print(myList)

# Print the number of items in the list: print() print("Length of list:") print(len(myList))

# Part 4: # Use the count() method to print the number of times # the number 7 occurs in the list: print() print("Using count() to find the number of times 7 occurs in the list:") # # Make sure your code prints your result.

# Part 5: # Use the pop() method to remove the last item from the list: # The last item is the one at index len(myList) - 1. print() print("Using pop() to remove the last item from the list:") # print(myList)

# Part 6: # Use the remove() method to remove the number 7 from the list: # This will remove the first (and only) 7 it finds. print() print("Using remove() to remove the number 7 from the list:") # print(myList)

# Part 7: # Use the insert() method to insert the number 7 back in the list: # Insert it where it is supposed to go, which is at index 6. print() print("Using insert() to insert the number 7 back in the list:") # print(myList)

# Part 8: # Use the index() method to find the index of the number 7 in the list: # This will be for the first (and only) 7 it finds. print() print("Using index() to find the index of the number 7 in the list:") # # Make sure your code prints your result.

# Part 9: # Use the min() function to find the smallest item in the list: print() print("Using min() to find the smallest item in the list:") # # Make sure your code prints your result.

# Part 10: # Use the max() function to find the largest item in the list: print() print("Using max() to find the largest item in the list:") # # Make sure your code prints your result.

# Part 11: # Use the "in" operation to determine whether the number 7 is in the list: print() print("Using \"in\" to determine whether the number 7 is in the list:") # # Make sure your code prints your result.

# Part 12: # Use the "in" operation to determine whether the number 13 is in the list: print() print("Using \"in\" to determine whether the number 13 is in the list:") # # Make sure your code prints your result.

# Part 13: # Use the following kind of "for" loop # for x in myList: # to print each item in the list on a separate line: print() print("Each item in list on a separate line:") # # Make sure your code prints your result.

# Part 14: # Use a "for" loop to add 100 to each item in the list: # Your "for" statement should be: # for i in range(len(myList)): # Inside your "for" loop you will use "i" as an index # to reference the corresponding list item. # # NOTE: The following code will NOT work here. # for x in myList: # x += 100 # The reason is because numbers are immutable and each # list entry itself is not changed. # Only the x references are changed. # print() print("Add 100 to each item in the list:") # print(myList)

# Part 15: # Use a "for" loop to subtract 50 from each item in the list: print() print("Subtract 50 from each item in the list:") # print(myList)

# Part 16: # Use the reverse() method to reverse the items in the list: print() print("Using reverse() to reverse the items in the list:") # print(myList)

# Part 17: # Use the sort() method to sort the items in the list: # The items are to be sorted back into their original ascending order. print() print("Using sort() to sort the items in the list:") # print(myList)

# Use the copy() method to create a shallow copy of the list: # Name your copy copyList. # Shallow copies can lead to problems when the items are mutable. # For our list, its number items are immutable (not mutable). print() print("Using copy() to create a shallow copy of the list:") copyList = myList.copy() print(copyList)

# Part 18: # Use the clear() method to remove all the items from myList: print() print("Using clear() to remove all the items from myList:") # print("myList:", myList) print("copyList:",copyList)

# Part 19: # Use the extend() method to extend myList with the contents of copyList: print() print("Using extend() to extend myList with the contents of copyList:") # print("myList:", myList) print("copyList:",copyList)

Assignment:

In the Python shell, you can use the following command to view the list class public interface: dir(list) You can use the following command to view descriptions for the list class methods: help(list) Make sure you test your program each time you add the code for a given part. This is called incremental testing. It is better to find errors early so you do not repeat them. YOU ARE NOT ALLOWED TO HARD-CODE AN ANSWER TO ANY PART. For example, for part 1, you cannot have: print (1) print (7) print (12) For this part, you should use list indexing to print each item. YOU ARE NOT ALLOWED TO HARD-CODE THE LENGTH OF THE LIST. For example, for part 2, you cannot have: print (12) For this part, you should use the len() function. YOU ARE NOT ALLOWED TO RE-INITIALIZE myList IN ANY PART. Your code MUST progressively modify myList as specified in the comments. Do NOT write code in any part like: myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] or myList = [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] or myList = anything else

What outputs supposed to look like the expected one:

myList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] Items at indexes 0, 6, and 11: 1 7 12 Length of list: 12 Using append() to add the number 7 to the end of the list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 7] Length of list: 13 Using count() to find the number of times 7 occurs in the list: 2 Using pop() to remove the last item from the list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] Using remove() to remove the number 7 from the list: [1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12] Using insert() to insert the number 7 back in the list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] Using index() to find the index of the number 7 in the list: 6 Using min() to find the smallest item in the list: 1 Using max() to find the largest item in the list: 12 Using "in" to determine whether the number 7 is in the list: True Using "in" to determine whether the number 13 is in the list: False Each item in list on a separate line: 1 2 3 4 5 6 7 8 9 10 11 12 Add 100 to each item in the list: [101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112] Subtract 50 from each item in the list: [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62] Using reverse() to reverse the items in the list: [62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51] Using sort() to sort the items in the list: [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62] Using copy() to create a shallow copy of the list: [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62] Using clear() to remove all the items from myList: myList: [] copyList: [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62] Using extend() to extend myList with the contents of copyList: myList: [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62] copyList: [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!