Lecture 2 Flashcards
Get identifier of an object a, test equality of a and b
id(a), a is b
Immutable objects
tuples, strings, floats, ints (a[2]=… returns an error!)
Mutable objects
lists, dictionaries
Indexing
a[i:j:k], start, stop (not included), step
Copy a list a
a.copy()
John = 'computer_science' Tim = John Tim += ', math' Anna = ['electrical'] Julie = Anna Julie += ['physics'] print(John, Anna)
computer_science [‘electrical’, ‘physics’]
A = [1,2,3,4] B = A C = A
B.append(5)
C = C + [6]
B.append(6)
A==B==C
A is B but A is not C!
Pop method?
A.pop(i) returns the ie element of the list and removes it.
Modify a list inside a function
list[:] = …
Loop on index, value
for i, v in enumerate(a)
Loop on key, value (dictionary)
for k, v in a.items()
Lowercase a string
a.lower()
Apply a function to each element of a list
list(map(fct, a))
Split a string into list of words
a.split()
Create list of tuples from two lists
list(zip(a1, a2))