Purdue CNIT 155 Final Exam (Python)

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/198

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

199 Terms

1
New cards

What is a List?

A data structure that can store many values of under one name

2
New cards

Which is an example of how to create an empty list?

Variable = []

Variable = list()

3
New cards

Which is an example of how to create a list with predefined values?

colors = ["red, "yellow", "blue"]

4
New cards

To create a list with predefined values, you have to....

Put a sequence of items in []

5
New cards

What does list1.append(elem) do?

Adds a single element to the end of the list

6
New cards

What does list1.insert(index, elem) do?

Inserts the element elem at the given index, shifting elements to the right

7
New cards

What does list1.extend(list2) do?

Add the elements in list2 to the end of the list.

8
New cards

T or F: The extend method is not only used with a list.

False

9
New cards

T or F: Using + or += on a list is similar to using the extend method.

True

10
New cards

How do you access the individual element in a list using an index?

list1[index]

11
New cards

What are characteristics of Positive Indexes?

First element in the list has index 0, second element has index 1, and n'th element is n-1

12
New cards

What are characteristics of Negative Indexes?

Identify positions relative to the end of the list, index -1 identifies the last element, and -2 identifies the next to last element, etc.

13
New cards

What does Function do?

Is a description

14
New cards

What does len do?

Returns the number of items in the list

15
New cards

What does min do?

Returns the minimum among the elements in the list

16
New cards

What does max do?

Returns the maximum among the elements in the list

17
New cards

What does sum do?

Returns the sum of all the elements in the list. (applies to list of numerical values only)

18
New cards

Given a list, you may use ________ to randomize the order of the elements in a list.

Shuffle

19
New cards

What do you type to import the shuffle function and use the shuffle function with a list?

from random import shuffle

shuffle(list1)

20
New cards

You can refer to elements of a list using the...

Subscript syntax

21
New cards

T or F: The code below makes third have a value of 83.0

scores = [75.0, 68.5, 83.0]

third = scores[2]

True

22
New cards

T or F: Negative indices count from the right end.

True

23
New cards

What is the value of third after running the code below?

scores = [75.0, 68.5, 83.0]

third = scores[-1]

83.0

24
New cards

Which is an example of negative indices?

scores = [75.0, 68.5, 83.0]

third = scores[-1]

25
New cards

Which is an example of positive indices?

scores = [75.0, 68.5, 83.0]

third = scores[2]

26
New cards

Slicing gives a...

List of elements

27
New cards

Subscripting gives a...

Single element

28
New cards

Which is an example of slicing a list?

scores = [68.5, 83.0]

exams = scores[1:3]

29
New cards

T or F: Each list is an object.

True

30
New cards

Which is an example of how you call a list method?

list.methodName(arguments)

31
New cards

T or F: The in operator does not check for that exact method, and looks "inside" elements.

False

32
New cards

To search for an element in a list, you can use the...

in operator and index method

33
New cards

Which is an example of using the in operator to search for an element in a list?

Names = ["William", "Scott", "Sophia"]

if "William" in Names:

34
New cards

Which is an example of using the index method to search for an element's position in a list?

Names = ["William", "Scott", "Sophia"]

loc = Names.index("Scott")

# loc is 1

35
New cards

Which is an example of using the index method to search for an element from the start to the end of a list?

List.index(elem, start, end)

36
New cards

T or F: If the index method finds the element, it return its index (position) inside the list.

True

37
New cards

T or F: A ValueError is given when the index method searches for the element and the element is not between start and end in the list.

True

38
New cards

T or F: A Searchgive Error is not given when the index method is used and the element is NOT found.

False

39
New cards

Which is an example of transversing a list with a for loop?

scores = [ 85, 72, 56, 98, 84, 72]

for grade in scores:

print(grade)

40
New cards

To get both indices and elements in transversing a list, use...

A loop controlled with range(len(list)) with subscripting

for index in range(len(scores)):

print (index,"\t"

,grade[i])

Enumerate(mylist) to control the

for loop

for index, grade in enumerate(scores):

print (index,"\t"

,grade)

41
New cards

Which is an example of using the extend method to add a whole list to the end of a list?

scores = [ 85, 72, 56, 98, 84, 72]

scores.extend([55, 88, 79])

42
New cards

What will be the result of the code below?

Names = ["William", "Scott", "Sophia"]

Names.insert (2, "Hanah")

Names = ["William", "Scott", "Hannah", "Sophia"]

43
New cards

T or F: Append, extend, and insert return something.

False

44
New cards

What will be the result of this code?

colors = colors.append("yellow")

ERROR

colors = None

45
New cards

What will be the result of this code?

colors.append("yellow")

Changes colors to have "yellow" at end

46
New cards

What will be the result of this code?

colors + primaries

ERROR

Longer list will be thrown out

47
New cards

What will be the result of this code?

colors = colors + primaries

Returns a new list

48
New cards

T or F: You can delete from a list by index.

True

49
New cards

Which is an example of deleting from a list using index?

del list[index]

50
New cards

Deleting from a list using index...

Removes the element at position index and shifts down the following elements to fill in the gap

51
New cards

Which is an example of deleting a range from a list by using a slice?

del list[2 : 5]

# removes elements at positions 2, 3, and 4

52
New cards

What does colors.remove("blue") do?

Searches for the first occurrence of "blue and deleters it

53
New cards

What does the "search and destroy" method do?

Searches for and removes a specific value from a list

54
New cards

What happens if the specific value can not be found using the "search and destroy" method?

Gives a runtime error

55
New cards

Which is an example fo the "search and destroy" method using del?

if "blue" in colors:

pos =

colors.index("blue")

del colors[pos]

56
New cards

What does the reverse method do to a list?

Reverses the order of a list

57
New cards

Which is an example of using the reverse method on a list?

mylist = ["red", "green", "blue"]

mylist.reverse()

print(mylist) gives ["blue", "green", "red"]

58
New cards

T or F: After using the reverse method on a list, the original order is lost.

True

59
New cards

T or F: The reverse method does not return a value.

True

60
New cards

What will be the result of this code?

mylist = mylist.reverse()

ERROR

mylist becomes None

61
New cards

Which is an example of a new reversed copy of the list? (original list remains unchanged)

backwards = list(reversed(mylist))

62
New cards

Reverse is a....

Method that mutates the list that is its argument

63
New cards

Reversed is a....

Function that returns a new sequence (Does not actually return a list, but you can convert it with the list typecast)

64
New cards

You can sort a list using the...

Sort method

65
New cards

The default to the sort method being used on a list with integers is...

Ascending order

66
New cards

Which is an example of the sort method being used on a list with integers?

scores = [ 75, 63, 92 ]

scores.sort()

print(scores)

# gives [63, 75, 92]

67
New cards

The default to the sort method being used on a list with strings is...

Alphabetic (ASCII) order

68
New cards

Which is an example of the sort method being used on a list with strings?

physicists = ["Einstein", "Newton", "Hawking"]

physicists.sort()

print(physicists)

# gives ["Einstein", "Hawking", "Newton"]

69
New cards

Which is an example of the sort method being used on a list with integers to sort it by descending order?

scores.sort(reverse = True)

print(scores)

# gives [92, 75, 63]

70
New cards

Which is an example of making a new sorted list and keeping the original list as well?

ordered_list = sorted(scores)

71
New cards

Which are different ways to create a list?

Hard code it, start out with an empt list and append it, start out with and empty list and concatenate to it, split a string, and/or replication

72
New cards

Which is the output of this code?

colors = ["red", "green", "blue"]

colors[0] = "purple"

colors = ["purple", "green", "blue"]

73
New cards

Which is the output of this code?

colors = ["red", "green", "blue"]

colors[3] = "yellow"

IndexError: list assignment is out of range

(Is the result if an index is greater than or equal to the length of the list)

74
New cards

T or F: It is not possible to have two variables referring to the very same list (aliasing)

False

75
New cards

How can two variables refer to the same list?

Arguments and parameters or by assigning one to the other

76
New cards

Which is an example of two variables referring to the same list?

testscores = [ 84, 100, 78]

myscores = testscores

# alias!

myscores.append(96)

print(testscores)

#gives[ 84, 100, 78, 96]

77
New cards

Functions which take lists as arguments and change them during execution are called __________.

Modifiers

78
New cards

The changes that functions make after taking lists as arguments and changing them during execution are called __________

Side Effects

79
New cards

Passing a list as an argument actually ______________ , not a copy of the list.

Passes a reference to the list

80
New cards

T or F: Lists are mutable.

True

81
New cards

T or F: Since lists are mutable, changes made to the elements referenced by the parameter change the same list that the argument is referencing

True

82
New cards

Which is an example of passing a list as an argument?

def doubleValues(aList):

#double its values in the list

for position in range(len(aList)):

aList[position] = 2 * aList[position]

numList = [2, 5, 9]

print(numList)

doubleValues(numList)

print(numList)

83
New cards

What is Scaling?

Multiplying all elements by the same number

84
New cards

What is Parameters?

A list and a scaling factor

85
New cards

What is Postconditions?

Mutates the list and returns nothing

86
New cards

T or F: Usually a mutating function needs to loop over elements, not indexes

False

87
New cards

T or F: Pretty much all list algorithms use a loop, which is typically a for loop and occasionally a while loop.

True

88
New cards

Which are built in functions or methods for list algorithms?

Sum, Count, Max/Min, Sort

89
New cards

What does the Sum function do to a list?

Adds together all the elements?

90
New cards

What does the Count function do to a list?

Finds the number of occurrences of a value

91
New cards

What does the Max/Min function do to a list?

Finds the largest/smallest value

92
New cards

What does the Sort function do to a list?

Rearranges the elements to be in order

93
New cards

What are characteristics that relate to needing an accumulator with an initial value to use the sum function on a list?

Zero is the additive identity (adding zero doesn't change anything), one is the multiplicative identity, for a string, it's the "" empty string for concatenation, for a list, it's the [] empty list for concatenation or appending

94
New cards

T or F: Variations are built-in functions.

False

95
New cards

The algorithm for the sum function being used on a list is....

1. Initialize the accumulator to the identity factor for the operation involved

2. For each element in the list, add it to the accumulator (or whatever operation is called for)

96
New cards

The algorithm for the count function being used on a list is....

1. Initialize the counter to zero

2. For each element in the list: 1 if it equals the search value, add 1 to the counter

97
New cards

Variation of the count function on a list...

Count the elements with a certain property (>0, even. etc.)

98
New cards

The algorithm for the max/min function being used on a list is....

1. Initialize the "best" variable to the first element of the list

2. For each element in the rest of the list: .1 if its bigger than the best, it's the new best

99
New cards

T or F: To use the man/min function on a list, elements must be comparable (all strings or all numbers, not a mix of types).

True

100
New cards

Variation of the count function on a list...

Location fo the maximum, maximum of a function applied to each element