









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The fundamental concepts and operations related to python lists. It provides a comprehensive overview of list data structures, including how to create, access, modify, and manipulate lists. Essential list operations such as indexing, slicing, appending, inserting, removing, and sorting elements. It also addresses common list-related tasks, such as finding the length of a list, checking if an element is in a list, and iterating over list elements. Additionally, the document includes examples and explanations of various list-related code snippets, helping users understand the practical applications of list operations in python programming. This resource is suitable for both beginner and intermediate python learners, providing a solid foundation for working with lists and preparing for related exams or assignments.
Typology: Exams
1 / 17
This page cannot be seen from the preview
Don't miss anything!










What is a list in Python? - correct answers ✅a container that stores a collection of elements that are arranged in a linear or sequential order Given the following list, what value is at index 5? values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] - correct answers ✅ 6 Consider the following line of code: somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] Which one of the following options is a valid line of code for displaying the element at the twenty-second position in the list? - correct answers ✅print(somelist[21]) Given the list below, what are the upper and lower bounds? somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] - correct answers ✅0, 25 What is the valid range of index values for a list? - correct answers ✅at least zero and less than the number of elements in the list What is printed after executing the following code snippet? somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z"] for i in range(len(somelist)) : print(somelist[i], end = "") - correct answers ✅abcdefghijklmnopqrstuvwxyz What is printed after executing the following code snippet? somelist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] for i in range(0, len(somelist), 2) : print(somelist[i], end = "") - correct answers ✅acegikmoqsuwy Which statement correctly creates a list that contains four elements? - correct answers ✅values = [1, 2, 3, 4] What is the difference between a list and a string? - correct answers ✅lists can hold values of any type, whereas strings are only sequences of characters Which statement gets the length of the list values? - correct answers ✅numValues = len(values) Given a list values that contains the first ten prime numbers, which statement prints the index and value of each element in the list? - correct answers ✅for i in range(10): print(i, values[i])
What is displayed when it runs? - correct answers ✅[1, 2, 3, 6, 5] What will happen when the following code segment is executed? values = [1.618, 2.71, 3.14] print(values[3]) - correct answers ✅Python will display an out-of-range error Consider the following code snippet. What will be stored in the list prices after this code executes? prices = [10.00, 15.50, 13.50, 20.15] for i in range(len(prices)) : prices[i] = prices[i] * 1.06 - correct answers ✅[10.60, 16.43, 14.31, 21.36] Given the list values = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], which statement fills the list with these numbers: 1 2 3 4 5 6 7 8 9 10 - correct answers ✅for i in range(10) : values[i] = i
Given the following code snippet, which statement correctly copies the contents of the list prices? prices = [10.00, 15.50, 13.25, 20.15] - correct answers ✅values = list(prices) Given the definition of a list: names = [] which statement correctly adds "Harry"to the list? - correct answers ✅names.append("Harry") Which of the following statements looks for the name 'Bob' in the list names? - correct answers ✅if "Bob" in names: print("found Bob") Given the following code snippet, what is the value of the variable indexValue? states = ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"] indexValue = states.index("Hawaii") - correct answers ✅ 1 Given the following code snippet, what is the value of the variable removedValue? states = ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"] removedValue = states.pop(3) - correct answers ✅Maine Given the following code snippet, what is the content of the list states? states = ["Alaska", "Hawaii", "Florida", "Maine", "Ohio", "Florida"] states.pop() - correct answers ✅["Alaska", "Hawaii", "Florida", "Maine", "Ohio"]
What is the resulting value of this code snippet? sum([1, 2, 3, 4, 5]) - correct answers ✅ 15 What is the resulting value of this code snippet? max(["Roland", "Kent", "Ravi", "Amy"]) - correct answers ✅"Roland" What is the resulting content of the list after this code snippet? min(["Roland", "Kent", "Ravi", "Amy"]) - correct answers ✅Amy What is the resulting content of the list values after this code snippet? values = [1991, 1875, 1980, 1965, 2000] values.sort() - correct answers ✅[1875, 1965, 1980, 1991, 2000] What is the resulting content of the list letters after this code snippet? letters = list("abcdefg") - correct answers ✅letters contains a list with the contents: ["a", "b", "c", "d", "e", "f", "g"] Which of the following code segments will result in values containing the list["Hydrogen", "Helium", "Lithium"]? - correct answers ✅values = [] values.append("Hydrogen") values.append("Helium") values.append("Lithium")
Which of the following code segments will result in values containing the list["X", "Y", "Z"]? - correct answers ✅values = ["X"] values.insert(1, "Y") values.insert(2, "Z") What is stored in pos when the following code segment executes? values = ["J", "Q", "X", "Z"] pos = values.find("Y") - correct answers ✅No value is stored because a runtime exception occurs What is in grades after the follow code segment executes? grades = ["A", "B+", "C", "C+", "C", "B-"] grades.remove("C") - correct answers ✅["A", "B+", "C+", "C", "B-"] What is displayed by the following code segment? values = ["Q", "W", "E", "R", "T", "Y"] print(min(values)) - correct answers ✅E What is output by the following code segment? x = list("QWERTY") print(x) - correct answers ✅["Q", "W", "E", "R", "T", "Y"]
Which statement correctly compares these two lists for equality? nums = [1, 2, 3, 4, 5] nums2 = [5, 4, 3, 2, 1] - correct answers ✅if nums == nums What is in values after the following code segment executes? values = [1, 2, 3] values = values * 4 - correct answers ✅Nothing because a runtime error occurs Consider the following code segment: x = [5, 3, 7] y = sorted(x) print(x) What is displayed when it executes? - correct answers ✅[5, 3, 7] Consider the following code segment: x = [5, 3, 7] y = x.pop() print(y) What is displayed when it executes? - correct answers ✅ 7 Consider the following code segment: x = [5, 3, 7, 9, 1] y = x[2 : 4] print(y) What is displayed when it executes? - correct answers ✅[7, 9] Consider the following code segment: x = [5, 3, 7, 9, 1] y = x[3 : ] print(y) What is displayed when it executes? - correct answers ✅[9, 1] Consider the following code segment: x = [5, 3, 7, 9, 1] y = x[ : 2] print(y) What is displayed when it executes? - correct answers ✅[5, 3]
What is the resulting contents of the list values in this code snippet? values = [] for i in range(10) : values.append(i) - correct answers ✅[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] What is the resulting value of the variable sentence in this code snippet? words = ["Mary", "had", "a", "little", "lamb"] sentence = "" for word in words : sentence = sentence + word - correct answers ✅Maryhadalittlelamb What is the resulting value of the variable sentence in this code snippet? words = ["Mary", "had", "a", "little", "lamb"] sentence = str(words) - correct answers ✅['Mary', 'had', 'a', 'little', 'lamb'] What is missing from this code snippet to find the largest value in the list? largest = values[0] for i in range(1, len(values)) : if values[i] > largest : - correct answers ✅largest = values[i] What code completes this code snippet to swap the first and last element in the list? states = ["Alaska", "Hawaii", "Florida", "Maine"] i = 0 ________________________ temp = states[j] states[j] = states[0] states[i] = temp - correct answers ✅j = len(states) - 1
Which code segment swaps the elements at position 0 and position 2 within thevalues list? - correct answers ✅temp = values[2] values[2] = values[0] values[0] = temp The following code segment is supposed to read all of the integers entered by the user until a blank line is entered and store them in values. values = [] inputStr = input("Enter a value (blank line to quit): ") ____________________ values.append(int(inputStr)) inputStr = input("Enter a value (blank line to quit): ") What line of code should be placed in the blank to achieve this goal? - correct answers ✅while inputStr != "" : What is missing from this code snippet to find the longest value in the list? colors = ["red", "purple", "blue", "green", "yellow", "light green"] longest = colors[0] for i in range(1, len(colors)) : _____________________ longest = colors[i] - correct answers ✅if colors[i] > longest : What type of search inspects every element sequentially?
this code snippet causes an IndexError: list index out of range error. What is printed by the following code snippet? words = ["Today", "is", "Wednesday", "tomorrow", "is", "Thursday"] i = 0 while i < (len(words)) : word = words[i] if len(word) < 8 : words.pop(i) else : i = i + 1 print(words) - correct answers ✅["Wednesday", "tomorrow", "Thursday"] Which of the following statements is NOT true about functions and lists: - correct answers ✅when calling a function with a list argument, the function receives a copy of the list The following function is supposed to add 1 to every element in a list of integers. def addOne(values) : for i in range(len(values)) : values[i] = values[i] + 1 What is wrong with the following function? - correct answers ✅The statement return values must be added to the end of the function. Given a list containing prices, how do you find the highest priced item and remove it from the list: - correct answers ✅find the maximum, remove it from the list What does the following code segment do? x = 0 for i in range(1, len(values)) : if values[i] > values[x] : x = i -
What does the following code segment do? x = [] for i in range(5) : x.append([]) for j in range(3) : x[i].append(0) - correct answers ✅It creates a table with 3 columns and 5 rows and stores it in x. What output is generated by the following code segment? table = [["T", "U", "V"], ["W", "X", "Y"]] print(table[0]) - correct answers ✅["T", "U", "V"] What output is generated by the following code segment? table = [["T", "U", "V"], ["W", "X", "Y"]] print(table[1][1]) - correct answers ✅X Consider the following table: table = [["T", "U", "V"], ["W", "X", "Y"]] Which statement will display Y? - correct answers ✅print(table[len(table) - 1][len(table[1]) - 1]) Which of the following statements best represents a deck of cards? - correct answers ✅suits = ["Hearts", "Spades", "Diamonds", "Clubs"] faceValues = ["Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"] What is printed from the following code snippet? prices = [[ 1.0, 3.50, 7.50 ], [ 10.0, 30.50, 70.50 ], [ 100.0, 300.50, 700.50 ], [ 1000.0, 3000.50, 7000.50 ]] print(prices[1][2])
Given the list values = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], which statement fills the list with these numbers: 1 4 9 16 25 36 49 64 81 100 - correct answers ✅for i in range(10): values[i] = (i + 1) * (i + 1) What list is stored in x after this code segment has run? x = [] for i in range(3) : x.append([]) for j in range(i) : x[j].append(0) - correct answers ✅[[0, 0], [0], []] How many values does a stereo sound file have for each sample? - correct answers ✅idk What is returned when scipi.io.wavfile.read is called? - correct answers ✅idk