Tuples and Lists Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Tuples

A

there are different data types: string, integer and float. These data types can all be contained in a tuple as follows:

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

concatenate tuples

A

tuple2 = tuple1 + (“hard rock”, 10)
tuple2

(‘disco’, 10, 1.2, ‘hard rock’, 10)

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

slicing tuples

A

tuple2[0:3]

‘disco’, 10, 1.2

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

sort tuples

A

RatingsSorted = sorted(Ratings)
RatingsSorted

[0, 2, 5, 6, 6, 8, 9, 9, 10]

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

Lists

A

Lists contain strings, floats and integers
L = [“Michael Jackson”, 10.1,1982,”MJ”,1]
L
[‘Michael Jackson’, 10.1, 1982, ‘MJ’, 1]

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

Use “extend” to add new elements to the list

A

L = [ “Michael Jackson”, 10.2]
L.extend([‘pop’, 10])
L
[‘Michael Jackson’, 10.2, ‘pop’, 10]

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

Use “append” to add one element

A

L = [ “Michael Jackson”, 10.2]
L.append([‘pop’, 10])
L
[‘Michael Jackson’, 10.2, [‘pop’, 10]]

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

Changing a list

A
A = ["disco", 10, 1.2]
print('Before change:', A)
A[0] = 'hard rock'
print('After change:', A)
Before change: ['disco', 10, 1.2]
After change: ['hard rock', 10, 1.2]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Deleting an element in a list

A
print('Before change:', A)
del(A[0])
print('After change:', A)
before change: ['hard rock', 10, 1.2]
After change: [10, 1.2]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

split

A

‘hard rock’.split()

[‘hard’, ‘rock’]

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

clone a list

A

B = A[:]
B
[‘banana’, 10, 1.2]

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