chapter 4 Flashcards

1
Q
  1. What is []?
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. 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].)
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. What does spam[int(‘3’ * 2) / 11] evaluate to?

For the following three questions, let’s say spam contains the list [‘a’, ‘b’, ‘c’, ‘d’].

A
  1. ‘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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

let’s say spam contains the list [‘a’, ‘b’, ‘c’, ‘d’].

  1. What does spam[-1] evaluate to?
A
  1. ‘d’ (Negative indexes count from the end.)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. What does spam[:2] evaluate to?

let’s say spam contains the list [‘a’, ‘b’, ‘c’, ‘d’].

A
  1. [‘a’, ‘b’]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

let’s say bacon contains the list [3.14, ‘cat’, 11, ‘cat’, True].

  1. What does bacon.index(‘cat’) evaluate to?
A
  1. 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

let’s say bacon contains the list [3.14, ‘cat’, 11, ‘cat’, True].

A
  1. [3.14, ‘cat’, 11, ‘cat’, True, 99]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

bacon contains the list [3.14, ‘cat’, 11, ‘cat’, True].

Q: 8. What does bacon.remove(‘cat’) make the list value in bacon look like?

A
  1. [3.14, 11, ‘cat’, True]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. What are the operators for list concatenation and list replication?
A
  1. The operator for list concatenation is +, while the operator for replication is *.
    (This is the same as for strings.)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. What is the difference between the append() and insert() list methods?
A
  1. While append() will add values only to the end of a list, insert() can add them
    anywhere in the list.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. What are two ways to remove values from a list?
A
  1. The del statement and the remove() list method are two ways to remove
    values from a list.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Name a few ways that list values are similar to string values. (jest ich 5 tutaj)
A

Both lists and strings can be passed to len(),

have indexes and slices,

be used in for loops,

be concatenated or replicated,

be used with the in and not
in operators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. What is the difference between lists and tuples?
A
  1. Lists are mutable(można je zmienić); 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 ].
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. How do you type the tuple value that has just the integer value 42 in it?
A
  1. (42,) (The trailing comma is mandatory.)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. How can you get the tuple form of a list value? How can you get the list form of a tuple value?
A
  1. The tuple() and list() functions, respectively
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. Variables that “contain” list values don’t actually contain lists directly. What do they contain instead?
A
  1. They contain references to list values.
17
Q
  1. What is the difference between copy.copy() and copy.deepcopy()?
A
  1. 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.