Python Equality versus Identity ("is" operator) Flashcards

 

card 1 of 12

 

>>> a = b = 5
>>> a == b
>>> a is b

 

the 'is' operator compares the origin of two things

True
True

card 2 of 12

 

>>> a = b = [0]
>>> a == b
>>> a is b

 

the 'is' operator compares the origin of two things

True
True

card 3 of 12

 

>>> a = [0]
>>> b = [5-5]
>>> a == b
>>> a is b

 

the 'is' operator compares the origin of two things

True
False

card 4 of 12

 

>>> a = 5
>>> b = 5
>>> a == b
>>> a is b

 

this might see like Python is wrong

True
True

card 5 of 12

 

>>> a = "five"
>>> b = "five"
>>> a == b
>>> a is b

 

this might see like Python is wrong

True
True

card 6 of 12

 

>>> a = {5:"five"}
>>> b = {5:"five"}
>>> a == b
>>> a is b

 

the 'is' operator compares the origin of two mutable variables

True
False

card 7 of 12

 

>>> a = b = {5:"five"}
>>> a == b
>>> a is b

 

the 'is' operator compares the origin of two mutable variables

True
True

card 8 of 12

 

>>> a = {5:"five"}
>>> b = {5:"five"}
>>> b[2] = "two"
>>> a
>>> b
>>> a == b

 

assignment can add an entry to a dictionary

{5:'five'}
{2:'two', 5:'five'}
False

card 9 of 12

 

>>> a = b = {5:"five"}
>>> b[2] = "two"
>>> a
>>> b
>>> a == b

 

if 'a is b' is True, then assignment to one assigns to the other

{2:'two', 5:'five'}
{2:'two', 5:'five'}
True

card 10 of 12

 

>>> a = b = [15,2,0]
>>> Evt = [a,100]
>>> Fvt = [b,500]
>>> Evt[0].append(-1)
>>> Fvt

 

surprise, but the same principle (about 'is') is at work

[[15,2,0,-1], 500]

card 11 of 12

 

>>> a = [15,2,0]
>>> b = a
>>> a == b
>>> a is b
>>> not a is b

 

assigning from an existing variable shares origin

True
True
False

card 12 of 12

 

>>> a = [15,2,0]
>>> b = a[:]
>>> a == b
>>> a is b

 

recall that the slice '[:]' is a copy of the entire list

True
False
The '[:]' produces a copy, but it does not have the same origin

 

End of Problem Set

(use browser reload to restart this problem set)