Chapter 4: Lists

¡Supera tus tareas y exámenes ahora con Quizwiz!

An integer inside the square brackets that follow a list value is called the __________. (i.e. spam [3])

An integer inside the square brackets of a list value is called the index. (p.78)

What is the difference between append() and insert() list methods?

Append() adds a list item to the end of the list. insert() adds an item anywhere in the list. Append() only has one parameter, the item being added. List() has two parameters. The location in the list, and the item being added.

How do you type the tuple value with just the integer value 42 in it?

(42, )

What are the operators for list concatenation and replication?

+ *

Consider the following list value. >>> spam = [['cat', 'bat'], [10, 20, 30, 40, 50]] What value would len(spam) return?

2 (p. 81)

A list begins with an __________ and ends with a __________. The items in a list are separated by __________.

A list begins with an open square bracket and ends with a closed square bracket. The items in a list are separated by commas. (p. 78)

A __________ is a value that contains multiple values in an ordered, sequence.

A list is a value that contains multiple values in an ordered, sequence. (p. 78)

A list value is a __________ data type: It can have values added, removed, or changed. A string is __________: It cannot be changed.

A list value is a mutable data type: It can have values added, removed, or changed. A string is immutable: It cannot be changed. (p. 94)

A __________ is a function that is tied to values of a certain data type.

A method is a function that is tied to values of a certain data type. (p. 88)

A __________ can get several list items in the form of a new list.

A slice can get several list items in the form of a new list. (p. 80)

What is the difference between lists and tuples?

Lists begin and end with square brackets. Tuples begin and end with parenthesis. Lists are mutable. Tuples are immutable.

Consider the following code: >>> spam = ['alpha', 'bravo', 'charlie', 'delta', 'echo'] >>> spam[-1] Which of the following will be returned from the above code? A. 'alpha' B. 'echo' C. 'spam' D. The code will result in an error.

B. 'echo' (p.80)

Name a few ways list values are similar string values?

Both a list and a string represent an ordered sequence. A list is an ordered sequence of items. A string is an ordered sequence of characters. Many of the functions/methods that work with lists also work with strings.

True or False: A slice goes up to and includes the the second index value.

False. A slice goes up to, but will not include, the second index value. (p. 80)

True or False: Index values of a list can be integers or floating point numbers.

False. Index values of a list can only be integers. (p. 79)

In a slice, the first integer is where the slice __________ and the second integer is where the slice __________.

In a slice, the first integer is where the slice begins and the second integer is where the slice ends. (p. 80)

What operator would you use to concatenate two lists together? What operator would you use to replicate a string?

The + operator is used to concatenate lists. (p.81) [1,2,3] + [3,4,5] returns [1,2,3,4,5,6] The * operator is used to replicate lists. (p. 81) [1,2,3] * 2 returns [1,2,3,1,2,3]

The __________ function will return a randomly selected item from a list.

The random.choice( ) function will return a randomly selected item from a list. (p. 86)

The __________ function will randomly shuffle the item in a list.

The random.shuffle( ) function will randomly shuffle the item in a list. (p. 87)

True or False: A list can contain another list within it.

True

True or False: The len() function will return the number of values that are in a list value passed to it, just like it can count the number of characters in a string value.

True (p. 81)

True or False: You can determine whether a value is or isn't in a list with the in and not in operators.

True (p. 85)

In what two ways is the tuples data type different than the list data type?

Tuples are typed with parenthesis instead of square brackets. Tuples are immutable where lists are mutable.

How can you get the list form of a tuple value?

Use the list() method with the tuple the argument.

How can you get the tuple form of a list value?

Use the tuple() method with the list as the argument.

Values inside a list are called __________.

Values inside a list are called items. (p. 78)

What is [ ] ?

[ ] is a square bracket that encloses a list. (p. 78)

spam contains the list ['a', 'b', 'c', 'd'] What does spam[:2] evaluate to?

['a', 'b'] [:2] is the same as [0:2] which is a slice of list spam from 0 up to but NOT including 2.

What is the difference between copy.copy() and copy.deepcopy() methods?

copy.copy() makes a copy of the list. If the list contains another list copy.deepcopy() makes copies of the inner list as well.

spam contains the list ['a', 'b', 'c', 'd'] What does spam[-1] evaluate to?

d

spam contains the list ['a', 'b', 'c', 'd'] What does spam[int(int['3'*2) // 11)] evaluate to?

d Since '3' is a string' 3'*2 returns '33' which is turned into the integer 33 with int, and divided by 11 returning 3. d is the third index of the string.

What are two ways to remove an item from a list?

del listName [itemPosition] listName.remove(itemName)

Consider the following code. >>> spam = ['cat', 'bat', 'rat', 'elephant'] How would you remove the item 'rat'?

del spam[2] (p. 82)

Variables that "contain" list values don't actually contain lists directly. They contain a __________ to the list instead.

reference

bacon = [3.14, 'cat', 11, 'cat', True] What does bacon.append(99) make the list value of bacon look like?

bacon = [3.14, 'cat', 11, 'cat', True, 99]

bacon = [3.14, 'cat', 11, 'cat', True] What does bacon.remove('cat' make the list value of bacon look like?

bacon = [3.14, 11, 'cat', True] Removes the first value of cat only.

bacon = [3.14, 'cat', 11, 'cat', True] What does bacon.index('cat') evaluate to?

1 The first 'cat' is in index position 1.

The __________ function is useful if you want both the item and the item's index in the loop's block.

The enumerate( ) function is useful if you want both the item and the item's index in the loop's block. (p. 86)

The first item in the list is an index __________.

The first item in the list is an index zero. spam[0] refers to the first item in the list spam. (p. 78)

The index __________ refers to the last item in a list.

The index [-1] refers to the last item in a list.

The index of the last item in a list with 5 items is _____________.

The index of the last item in a list with 5 items is [4]. (p. 78)

Consider the following list value. >>> spam = [['cat', 'bat'], [10, 20, 30, 40, 50]] What code would return the length of index [1]? What value would that code return?

len(spam[1]) 5

Consider the following list value. >>> spam = [['cat', 'bat'], [10, 20, 30, 40, 50]] What is the index value of ['cat', 'bat']?

spam [0]

Consider the following list value. >>> spam = [['cat', 'bat'], [10, 20, 30, 40, 50]] What is the index value of 'bat'? What is the index value of 40?

spam [0][1] is the index value of 'bat'. spam[1][4] is the index value of 40.

Consider the following code. >>> spam = ['cat', 'bat', 'rat', 'elephant'] What code would call the entire list? What code would call only the FIRST three items from the list? What code would call only the LAST three items from the list?

spam or spam[0:4] or spam[:] would call the entire list. spam[0:3] or spam[:3] would call only the FIRST three items. spam [1:4] or spam[1:] would call only the LAST three items.

What method would you use to add the item 'moose' to the end of a list called spam? spam = ['cat', 'bat', 'rat', 'elephant']

spam.append('moose') spam = ['cat', 'bat', 'rat', 'elephant', 'moose'] (p. 89)

What method would you use to add the item 'moose' to the third index of a list called spam? spam = ['cat', 'bat', 'rat', 'elephant']

spam.insert(3, 'moose') spam = ['cat', 'bat', 'rat', 'moose', 'elephant'] (p. 89)

What method would you use to remove the item 'bat' from the list called spam? spam = ['cat', 'bat', 'rat', 'elephant']

spam.remove('bat') spam = ['cat', 'rat', 'elephant']

What method would you use to sort the list spam in alphabetical order? spam = ['cat', 'bat', 'rat', 'elephant']

spam.sort( )

Consider the following code. >>> spam = ['cat', 'bat', 'rat', 'elephant'] How would you change the value of 'bat' to 'aardvark'?

spam[1] = 'aardvark' would change the list item values to ['cat', 'aardvark', 'rat', 'elephant'] (p. 81)

How would you assign the value 'hello' as the third value in a list stored in a variable named spam? Assume spam contains [2, 4, 6, 8, 10] (Hint: there are 2 ways.)

spam[2]= 'hello' would replace the current third value of 6. spam.insert[2, 'hello'] adds the value 'hello' the third value and shifts all other values to the right.


Conjuntos de estudio relacionados

MHR 416 Mini-HW Ch. 12 - SmartBook - The Benefit Determination Process

View Set

MSE Quiz and Practice Test Questions

View Set

Basic Characteristics of Consumer Cognition (memory/knowledge)

View Set

Annual Security and Counterintelligence Awareness

View Set

(Chapter 13) How Safe Is Our Food Supply?

View Set

Chapter 2 - Rights in Real Estate

View Set

Chapter 14: Family Assessment and Interventions - PrepU

View Set