#### Python Double Indexing Flashcards

card 1 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> type(M)
>>> len(M)

lists can contain items that are lists

list
3

card 2 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M

lists can contain items that are lists

[4, 5, 6, 7]

card 3 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> type(M)

lists can contain items that are lists

list (it's [8,9])

card 4 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> (M)

M is a list, so (M) makes sense

8

card 5 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> (M)[-1]

M is a list, so (M)[-1] makes sense

9

card 6 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M

M is a list, so M makes sense

9

card 7 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M+M+M

the + operator for lists is concatentation

[4, 5, 6, 7, 8, 9, 1, 2, 3]

card 8 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M is B

items in lists can be mutable objects

True

card 9 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M != B

with double indexing like M, evaluate left to right

False -- M is equal to B

card 10 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> len(M) + len(M) + len(M)

each item of M is a list

9 (it's 3 + 4 + 2)

card 11 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M = 7
>>> A

M is A (they are aliases of same thing)

[1,2,7]

card 12 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M + M + M

M is A (they are aliases of same thing)

7

card 13 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M + M + M

each item of M is a non-empty list

13

card 14 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M + M + M

each item of M is a non-empty list

16

card 15 of 29

>>> A = [1,2,3]
>>> B = [4,5,6,7]
>>> M = [A,B,[8,9]]
>>> M + M + M

each item of M is a non-empty list

IndexError (list of M has only two items)

card 16 of 29

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = []
for e in M:
S = S + e
print(S)

each item of M is a non-empty list

[1, 2, 3, 4, 5, 6, 7, 8, 9]

card 17 of 29

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 0
for e in M:
S = S + e
print(S)

each item of M is a non-empty list

Error - can't use + with number and list

card 18 of 29

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 0
for e in M:
S = S + e
print(S)

each item of M is a non-empty list

13 (equals 1 + 4 + 8)

card 19 of 29

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 0
for e in M:
S = S + e
print(S)

each item of M is a non-empty list

16 (equals 2 + 5 + 9)

card 20 of 29

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 1
for e in M:
S = S * e
print(S)

each item of M is a non-empty list

90 (equals 2 * 5 * 9)

card 21 of 29

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
P = 1
for e in M:
P *= e
print(P)

each item of M is a non-empty list

90 (equals 2 * 5 * 9)

card 22 of 29

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
P = 1
for e in M:
P *= sum(e)
print(S)

each item of M is a non-empty list

918 (equals sum(M) * sum(M) * sum(M) )

card 23 of 29

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 0
for C in M:
P = 1
for v in C:
P = P * v
S = S + P
print(S)

S hints at sum, P hints at product

918 (equals 1*2*3 + 4*5*6*7 + 8*9)

card 24 of 29

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 0
for i in range(len(M)):
P = 1
for v in M[i]:
P = P * v
S = S + P
print(S)

S hints at sum, P hints at product

918 (equals 1*2*3 + 4*5*6*7 + 8*9)

card 25 of 29

A = [1,2,3]
B = [4,5,6,7]
M = [A,B,[8,9]]
S = 0
for i in range(len(M)):
P = 1
for j in range(len(M[i])):
P = P * M[i][j]
S = S + P
print(S)

S hints at sum, P hints at product

918 (equals 1*2*3 + 4*5*6*7 + 8*9)

card 26 of 29

A = [1,2,3]
B = [4,5,6]
M = [A,B,[7,8,9]]
S = 0
for i in range(len(M)):
P = 1
for j in range(len(M[i])):
P = P * M[j][i]
S = S + P
print(S)

Tricky! Product changes row in each iteration

270 (equals 1*4*7 + 2*5*8 + 3*6*9)

card 27 of 29

>>> A = "once"
>>> B = "twice"
>>> M = [A,B,"thrice"]
>>> M + M

each item of M is a non-empty sequence (string)

'ci'

card 28 of 29

>>> A = "once"
>>> B = "twice"
>>> M = [A,B,"thrice"]
>>> M = 'k'
>>> B

each item of M is a non-empty sequence (string)

Error! Strings are immutable

card 29 of 29

M = "one four twentyfive thousandmillion".split()
A = ""
for i in range(len(M)):
A = A + M[i][i:2*i]
print(A)

figure out M[0:0], then M[1:2], then M[2:4], ...

oenusa

#### End of Problem Set

(use browser reload to restart this problem set)