Python 4

Ace your homework & exams now with Quizwiz!

True or False? A list is an ordered, linear data structure in which each element in the list is accessed by its location in the list.

True

True or False? List traversal is the process of accessing, one-by-one, each element in a list.

True

True or False? Zero-based indexing means that the index value of a list always starts with 0

True

Which of the following typical list operations does not need to be provided an index value? (a) append (b) insert (c) delete

a) append

Which of the following is the correct notation for a tuple of only one element? (a) (10,) (b) (10) (c) (,10,) (d) All of the above

a)(10,)

What should the value of k be for lst[k] to access the tenth element of list lst? (a) 9 (b) 9.0 (c) '9' (d) All of the above

a)9

Match the following list operations with their description. (a) retrieve ____ removes the item in a list a the specified index value (b) update ____ adds at new value in a list at the specified index value (c) insert ____ gets the value in the list at the specified index value (d) delete ____ adds a provided value to the end of the list (e) append ____ replaces the value at the specified index location and a provided value

a)d b)c c)a d)e e)b

For lst = [10, 20, 30, 40, 50], what is the value of lst after lst.insert(3, 0) is performed? (a) [10, 20, 0, 30, 0, 40, 50] (b) [10, 20, 30, 0, 40, 50] (c) [10, 20, 30, 40, 0, 50] (d) [10, 20, 30, 40, 50, 0]

b) [10, 20, 30, 0, 40, 50]

For lst = [10, 20, 30, 40, 50], what value is the value of lst after lst.append(0) is performed? (a) [0, 10, 20, 30, 40, 50] (b) [10, 20, 30, 40, 50, 0] (c) [0] (d) [ ]

b) [10, 20, 30, 40, 50, 0]

Which of the following is not a valid list in Python? (a) [85, 'sunny', 'June 5'] (b) [10; 20; 30] (c) ['one', 'two', 'three'] (c) all of the above

b) [10;20;30]

For lst = [10, 20, 30, 40, 50], what value is accessed by list[3]? (a) 30 (b) 40 (c) '30' (d) '40'

b)40

Examine the following Python code, numList = [1, 4, 6, 8, 10, 3, 12] for k in range(0, len(numList)): print(numList[k]) How many times does the above loop iterate? (a) 0 (b) 6 (c) 7 (d) 8

b)6

Which of the following is the correct way to access the fourth element of a list named lst? (a) lst[4] (b) lst[3] (c) lst['3'] (d) lst['4']

b)lst[3]

For fruit = ' banana', what is displayed by the following Python code? for k in range(0, len(fruit), 2): print(fruit[k], end='')

bin

For lst = [10, 20, 30, 40, 50], what is the resulting value of lst[2:4]? (a) [20, 30] (b) [20, 30, 40] (c) [30, 40] (d) [30, 40, 50]

c) [30,40]

1. Which of the following operations cannot be used with tuples? (a) len (b) slice (c) del (d) concatenation

c) del

For nested list lst = [ [10, 20, 30], [40, 50, 60] ], which of the following is the correct means of assessing the element with the value 40? (a) lst[0][0] (b) lst[0][1] (c) lst[1][0] (d) lst[2][1]

c) lst [1][0]

For lst = [10, 20, 30, 40, 50], what value is the value of lst after del lst[3] is performed? (a) [10, 20, 30] (b) [30, 40, 50] (c) [10, 20, 40, 50] (d) [10, 20, 30, 50]

d) [10,20,30,50]

Which of the following is not a characteristic of lists in Python? (a) mutable (b) variable length (c) may contain mixed-type elements (d) nonlinear

d) non linear

Write a function named displayGrades that is passed a list of grades in the range 0-100, and displays the grades from highest to lowest, each grade on a separate line. Note that the original list argument passed should be not be altered. You may use the built-in sort method in the function.

def displayGrades(lst): for k in range (0,100): lst.sort() lst.reverse print(lst[k])

1. Five basic operations performed on lists are __________, ___________, ___________, ______________, and _____________.

delete append update insert recieve

True or False? Lists must contain at least one element in Python

false

True or False? An empty list in Python is denote as ['']

false -> []

A linear data structure is simply referred to as a _______

list

Give Python code that inserts the value 35 in lst = [10, 20, 25, 40] so that the values in the list remain in numerical order.

lst.append(35) lst.sort() OR lst.insert(3,35)

Give Python code that replaces the value 25 in list lst = [10, 20, 25, 40] with the value 40

lst[2]=40

For some list lst, what will be the result of the execution of the instruction lst.insert(1, 3)? (a) insert the value 1 at the location of the third element in the list (b) insert the value 1 at the location of the fourth element in the list (c) replace the third element in the list with the value 1 (d) replace the fourth element in the list with the value

none of the above, insert the value of 3 at index location 1 (2nd element)

Give Python code that, for two lists of integers (of the same length) named nums1 and num2, creates a third list named nums3 containing the sum of the corresponding numbers in each list, as demonstrated below. num1 [1, 2, 3, 4] num2[10, 20, 30, 40] num3[11, 22, 33, 44]

num3=num1+num2

The built-in ________ function in Python can be used for generating a sequence of integers.

range

The ______________ operation in Python can be used to obtain a sublist (ubstring) or a given list (string).

retrieve

Lists, tuples and strings are collectively referred to as _______________ in Python.

sequence types

Lists in which the first index value is always 0 is referred to as ____

zero-based indexing

True or False? An empty tuple is denote as ( ) in Python

true

Regarding list comprehensions, which of the following correctly generates a list containing all the even numbers between 2 and 100, inclusive? (a) [x % 2 == 0 for x in [1,2,3,4,5,6,7,8,9]] (b) [x for x in [1,2,3,4,5,6,7,8,9] if x % 2 == 0] (c) [x for x in range(2, 100) if x % 2 == 0] (d) [x for x in range(2, 102) if x % 2 == 0]

(d) [x for x in range(2, 102) if x % 2 == 0]

The ___ operator symbol is used to represent the concatenation of two lists or strings.

+

What would be the range of index values for a list of 10 elements? (a) 0 - 9 (b) 1 - 10 (c) 0 - 10

a) 0-9

Give Python code to add up all the elements in a list of integers named nums, and displays the result.

sum=0 for k in range (0, len(nums)+1): sum=sum+k print('Your sum is:', sum)

True or False? Because lists can contain elements of any type, they can also contain lists as their elements, allowing for the creation of arbitrarily complex data structures.

true

True or False? For statements are used for the construction of definite loops.

true

True or False? In Python, square brackets are used to both create a list and access its individual elements.

true

True or False? Lists in Python as mutable, which means that the contents of the list may be altered.

true

True or False? The + operator is used to concatenate two lists or two strings into one.

true

True or False? The built-in range function in Python is used for generating a sequence of integers.

true

True or False? The for loop Is always the best iterative control structure to use when traversing over indexed data structures in Python.

true

True or False? Tuples are the same as lists in Python, with the exception that they are immutable

true

True or False? When a variable is assigned to another variable holding a list, each variable ends up referring to the same instance of the list in memory.

true

In Python, ____________ are the same as lists, except that their contents cannot be altered.

tuples


Related study sets

Chapter 10 - Assessing for Violence

View Set

Principles Of Biology 1 (Midterm Study Guide)

View Set

taxes, retirement, and other insurance concepts

View Set

Chapter 50 & 52 Female Reproductive Function and Assessments and Breast Disorders

View Set