Sequences: Lists and Tuples Flashcards

1
Q

(Fill-In) Python’s string and tuple sequences are _______—they cannot be modified.

A

immutable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

(True/False) The + operator’s sequence operands may be of any sequence type.

A

False. The + operator’s operand sequences must have the same type; otherwise, a TypeError occurs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

(True/False) A += augmented assignment statement may not be used with strings and tuples, because they’re immutable.

A

False. A += augmented assignment statement also may be used with strings and tuples, even though they’re immutable. The result is a new string or tuple, respectively.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

(True/False) Tuples can contain only immutable objects.

A

False. Even though a tuple is immutable, its elements can be mutable objects, such as lists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

(Fill-In) A sequence’s elements can be ________ by assigning the sequence to a comma-separated list of variables.

A

unpacked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

(True/False) The following expression causes an error:

’-‘ * 10

A

False: In this context, the multiplication operator (*) repeats the string (‘-‘) 10 times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

(True/False) Slice operations that modify a sequence work identically for lists, tuples and strings

A

False. Slice operations that do not modify a sequence work identically for lists, tuples and strings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

(Fill-In) Assume you have a list called names. The slice expression ______ creates a new list with the elements of names in reverse order.

A

names[::-1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

(True/False) You cannot modify a list’s contents when you pass it to a function.

A

False. When you pass a list (a mutable object) to a function, the function receives a reference to the original list object and can use that reference to modify the original list’s contents

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

(True/False) Tuples can contain lists and other mutable objects. Those mutable objects can be modified when a tuple is passed to a function.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

(Fill-In) To sort a list in descending order, call list method sort with the optional keyword argument ______ set to True.

A

reverse.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

(True/False) All sequences provide a sort method.

A

False. Immutable sequences like tuples and strings do not provide a sort method. However, you can sort any sequence without modifying it by using built-in function sorted, which returns a new list containing the sorted elements of its argument sequence.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

(Fill-In) The _______ operator can be used to extend a list with copies of itself.

A

*=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

(Fill-In) Operators ______ and ______ determine whether a sequence contains or does not contain a value, respectively.

A

in, not in

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

(Fill-In) To add all the elements of a sequence to the end of a list, use list method _______, which is equivalent to using +=.

A

extend

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

(Fill-In) For a list numbers, calling method ________ is equivalent to numbers[:] = [].

A

clear.

17
Q

(Fill-In) You can simulate a stack with a list, using methods ______ and _____ to add and remove elements, respectively, only at the end of the list.

A

append, pop

18
Q

Fill-In) To prevent an IndexError when calling pop on a list, first ensure that _______.

A

the list’s length is greater than 0.

19
Q

(Fill-In) A list comprehension’s ______ clause iterates over the specified sequence.

A

for.

20
Q

(Fill-In) A list comprehension’s ______ clause filters sequence elements to select only those that match a condition.

A

if.

21
Q

(Fill-In) A generator expression is ______—it produces values on demand

A

lazy.

22
Q

(Fill-In) _______, _______ and _______ are common operations used in functional-style programming

A

Filter, map, reduce.

23
Q

(Fill-In) A(n) _______ processes a sequence’s elements into a single value, such as their count, total or average.

A

reduction

24
Q

(True/False) The letter ‘V’ “comes after” the letter ‘g’ in the alphabet, so the comparison ‘Violet’ < ‘green’ yields False

A

False. Strings are compared by their characters’ underlying numerical values. Lowercase letters have higher numerical values than uppercase. So, the comparison is True.

25
Q

(Fill-In) Built-in function ______ returns an iterator that enables you to iterate over a sequence’s values backward.

A

reversed.

26
Q

(Fill-In) In a two-dimensional list, the first index by convention identifies the ______ of an element and the second index identifies ______ the of an element.

A

row, column

27
Q

(Fill-In) The ________ format specifier indicates that a number should be displayed with thousands separators.

A

comma (,)

28
Q

(Fill-In) A Matplotlib ______ object manages the content that appears in a Matplotlib window

A

Axes

29
Q

(Fill-In) The Seaborn function ____ displays data as a bar chart.

A

barplot

30
Q

(Fill-In) The Matplotlib function ______ displays a plot window from a script.

A

show.