Python: Automate the Boring Stuff (Chapter 4: List: Questions)

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

What does bacon.remove('cat') make the list value in bacon look like?

[3.14, 11, 'cat', True]

What does spam[-1] evaluate to?

'd' (Negative indexes count from the end.)

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

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.

What is the difference between lists and tuples?

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 empty list value, which is a list value that contains no items. This is similar to how '' is the empty string value.`

What are the operators for list concatenation and list replication?

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

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].)

spam[2] = 'hello' (Notice that the third value in a list is at index 2 because the first index is 0.)

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

'd' (Note that '3' * 2 is the string '33', which is passed to int() before being divided by 11. This eventually evaluates to 3. Expressions can be used wherever values are used.)

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

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

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

1

Summary

Lists are useful data types since they allow you to write code that works on a modifiable number of values in a single variable. Later in this book, you will see programs using lists to do things that would be difficult or impossible to do without them. Lists are a sequence data type that is mutable, meaning that their contents can change. Tuples and strings, though also sequence data types, are immutable and cannot be changed. A variable that contains a tuple or string value can be overwritten with a new tuple or string value, but this is not the same thing as modifying the existing value in place—like, say, the append() or remove() methods do on lists. Variables do not store list values directly; they store references to lists. This is an important distinction when you are copying variables or passing lists as arguments in function calls. Because the value that is being copied is the list reference, be aware that any changes you make to the list might impact another variable in your program. You can use copy() or deepcopy() if you want to make changes to a list in one variable without modifying the original list.

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

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 are two ways to remove values from a list?

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

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

The tuple() and list() functions, respectively

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

They contain references to list values.

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

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

What does spam[:2] evaluate to?

['a', 'b']

What does bacon.append(99) make the list value in bacon look like?

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


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

Field Service Management Fundamentals

View Set

ATI Learning System Gerontology Final Quiz

View Set

Chapter 6-7 Econ 102 Principles of Microeconomics

View Set

BIO 210 : Chapter 7 Skeletal System

View Set

American Government CLEP - ONLINE PRACTICE TEST 1-3

View Set

Cards, Cars, and Currency Lesson 2 Econ Lowdown

View Set