Python Assignment by Slice Flashcards

 

card 1 of 14

 

>>> RR = [20,12,-5,10,60]
>>> RR[2:3] = False
>>> RR

 

slice assignment replaces a slice with another list

TypeError: can only assign a list to a slice

card 2 of 14

 

>>> RR = [20,12,-5,10,60]
>>> RR[2:3] = [False]
>>> RR

 

slice assignment replaces a slice with another list

[20,12,False,10,60]

card 3 of 14

 

>>> RR = [20,12,-5,10,60]
>>> (i,j) = 2,5
>>> RR[i:j] = [False]
>>> RR

 

slice assignment replaces a slice with another list

[20,12,False]

card 4 of 14

 

>>> RR = [20,12,-5,10,60]
>>> RR[2:6] = [False]
>>> RR

 

slice specification is more permissive than indexing

[20,12,False]

card 5 of 14

 

>>> words = "the prism and the light".split()
>>> words[5-7:] = "some glass".split()
>>> words

 

slice notation defaults to end of list after the colon

['the','prism','and','some','glass']

card 6 of 14

 

>>> swath = range(5)
>>> swath[:3] = 5*[-1]
>>> swath

 

slice notation defaults to start of list before the colon

[-1,-1,-1,-1,-1,3,4]

card 7 of 14

 

>>> sponsor = "Acme Plumbing and Heating Supply".split()
>>> sponsor[-3:] = [ ]
>>> sponsor

 

slice assignment can replace a slice with the empty list

['Acme','Plumbing']

card 8 of 14

 

>>> V = range(10,17)
>>> n = 4
>>> W = range(n)
>>> V[n:] = W
>>> V

 

carefully evaluate variables and substitute for the assignment

[10,11,12,13,0,1,2,3]

card 9 of 14

 

>>> and = "for else False or True".split()
>>> and[1:2], and[4:] = (True,False)
>>> and

 

multiple assignment works just like variables

SyntaxError: 'and' is a reserved name in Python

card 10 of 14

 

>>> ant = "for else False or True".split()
>>> ant[1:2], ant[4:] = (True,False)
>>> ant

 

multiple assignment works just like variables

TypeError: slice assignment can only assign lists

card 11 of 14

 

>>> ant = "for else False or True".split()
>>> ant[1:2], ant[4:] = ([True],[False])
>>> ant

 

multiple assignment works just like variables

['for',True,'False','or',False]

card 12 of 14

 

>>> ant = "for else False or True".split()
>>> ant[1:2], ant[4:] = ([True],[False])
>>> ant

 

multiple assignment works just like variables

['for',True,'False','or',False]

card 13 of 14

 

>>> mir = range(10)
>>> mir[3:7] = mir[4:6] = [-1]
>>> mir

 

evaluate the assignments from left to right

[0,1,2,-1,-1,9]
this question is confusing
it is not good style to program this way

card 14 of 14

 

>>> tox = range(8)
>>> tox[:4] = "fast"
>>> tox

 

strings can behave like lists

['f','a','s','t',4,5,6,7]

 

End of Problem Set

(use browser reload to restart this problem set)