Chapter 10 - Lists and Tuples Flashcards

1
Q

How are lists similar to strings?

A
  • lists are sequences, and therefore support indexing and slicing (like strings).
  • lists are objects, and contain methods which you can call (just as strings do)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are lists different from strings?

A
  • lists may contain different data types.
  • lists are mutable, meaning they can be altered after they are created.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do we access a item in a list?

A

indexing and slicing.

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

How do we add an item to a list?

A

append
ex. x.append(19)

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

How do we delete an item from a list?

A

del
ex. del z[1]

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

What is the general syntax for selecting items with a list comprehension?

A

[x for x in sequence if expression]
Where sequence is any sequence and expression is an expression involving x. Each item x from sequence is selected and added to the resulting list if the expression involving x evaluates to true.

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

What is the general syntax for computing something using each item in a list?

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

What are the differences between a list and a tuple?

A
  • tuples are immutable.
  • tuples are written with parenthesis instead of square brackets.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In what type of situations would we want to use a tuple instead of a list?

A

Software engineering because tuples prevent accidental modification.

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