Comparison
is
# is test object id, it is true only if x and y are same objects
l = [1, 2, 3, 4]
l2 = l
l3 = [1, 2, 3, 4]
print id(l), id(l2)
if l is l2:
print 'l is l2' # print
else:
print 'l is not l2'
if l is l3:
print 'l is l3'
else:
print 'l is not l3' # print
==, compare values
# ==, compare value
l = [1, 2, 3, 4]
l2 = l
l3 = [1, 2, 3, 4]
if l == l2:
print 'l is l2' # print
else:
print 'l is not l2'
if l == l3:
print 'l is l3' # print
else:
print 'l is not l3'
#!/usr/bin/python
l = [1, 2, [3, 4]]
l2 = [1, 2, [3, 4]]
print 'ID: ', id(l), id(l2);# l and l2 have different ids
if l == l2:
print 'l == l2'# print
else:
print 'l != l2'
#!/usr/bin/python
a = 'pub'
b = ''.join(['p', 'u', 'b'])
print a == b # True
print a is b # False
cmp, compare values
has been deprecated
Reference