Chapter 4. Lists

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

The tuple() and list() functions, respectively

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

(42,) (The trailing comma is mandatory.)

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

The del statement and the remove() list method are two ways to remove values from a list.

What are two ways to remove values from a list?

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

What does bacon.append(99) make the list value in bacon look like? For this question, let's say bacon contains the list [3.14, 'cat', 11, 'cat', True].

1

What does bacon.index('cat') evaluate to? For this question, let's say bacon contains the list [3.14, 'cat', 11, 'cat', True].

[3.14, 11, 'cat', True]

What does bacon.remove('cat') make the list value in bacon look like? For this question, let's say bacon contains the list [3.14, 'cat', 11, 'cat', True].

'd'

What does spam[-1] evaluate to? For this question, let's say spam contains the list ['a', 'b', 'c', 'd'].

['a', 'b']

What does spam[:2] evaluate to? For the following three questions, let's say bacon contains the list [3.14, 'cat', 11, 'cat', True].

'd'

What does spam[int('3' * 2) / 11] evaluate to? For this question, let's say spam contains the list ['a', 'b', 'c', 'd'].

The empty list value, which is a list value that contains no items. This is similar to how '' is the empty string value.

What is [ ]?

spam[2] = 'hello'

2. 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].) For the following three questions, let's say spam contains the list ['a', 'b', 'c', 'd'].

Both lists and strings can be passed to len(), have indexes and slices, be used in for loops, be concatenated or replicated, and be used with the in and not in operators.

Name a few ways that list values are similar to string values.

They contain references to list values.

Variables that "contain" list values don't actually contain lists directly. What do they contain instead?

The operator for list concatenation is +, while the operator for replication is *. (This is the same as for strings.)

What are the operators for list concatenation and list replication?

The copy.copy() function will do a shallow copy of a list, while the copy.deepcopy() function will do a deep copy of a list. That is, only copy.deepcopy() will duplicate any lists inside the list.

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

Lists are mutable; they can have values added, removed, or changed. Tuples are immutable; they cannot be changed at all. Also, tuples are written using parentheses, ( and ), while lists use the square brackets, [ and ].

What is the difference between lists and tuples?

While append() will add values only to the end of a list, insert() can add them anywhere in the list.

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


संबंधित स्टडी सेट्स

Business Principles 1301: Chapter 1

View Set

Habitat Requirements of Wildlife

View Set

Inquizitive: Chapter 17. Social Policy

View Set

Entrepreneurship and Small Business Management

View Set

Chapter 3 - Breathing Gas and the Effects on Your Body

View Set

Immuny - ćw 5 (najważniejsze zagadnienia)

View Set