Lecture 2 Flashcards

1
Q

Get identifier of an object a, test equality of a and b

A

id(a), a is b

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

Immutable objects

A

tuples, strings, floats, ints (a[2]=… returns an error!)

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

Mutable objects

A

lists, dictionaries

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

Indexing

A

a[i:j:k], start, stop (not included), step

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

Copy a list a

A

a.copy()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
John = 'computer_science'
   Tim = John
   Tim += ', math'
   Anna = ['electrical']
   Julie = Anna
   Julie += ['physics']
   print(John, Anna)
A

computer_science [‘electrical’, ‘physics’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
A = [1,2,3,4]
B = A
C = A

B.append(5)
C = C + [6]
B.append(6)

A

A==B==C

A is B but A is not C!

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

Pop method?

A

A.pop(i) returns the ie element of the list and removes it.

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

Modify a list inside a function

A

list[:] = …

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

Loop on index, value

A

for i, v in enumerate(a)

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

Loop on key, value (dictionary)

A

for k, v in a.items()

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

Lowercase a string

A

a.lower()

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

Apply a function to each element of a list

A

list(map(fct, a))

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

Split a string into list of words

A

a.split()

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

Create list of tuples from two lists

A

list(zip(a1, a2))

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

Sort a list (ordering function and direction…)

A

a.sort(key=lambda x: x[i], reverse=True)