I'm faced with what might seem a trivial, but nevertheless very annoying conundrum. While checking for membership using the "not in" operator combined with Boolean "and" and "or" on a following dictionary:
data = {"name": "david", "age": 27, "income": "absurd"}
I've found that:
#1 - found
if "name" not in data:
print "not found"
else:
print "found"
#2 - found
if "name" not in data and "income" not in data:
print "not found"
else:
print "found"
#3 - found
if "name" and "income" not in data:
print "not found"
else:
print "found"
#4 - found
if "name" not in data or "income" not in data:
print "not found"
else:
print "found"
#5 - NOT found (though it should be?)
if "name" or "income" not in data:
print "not found"
else:
print "found"
For my money #4 and #5 are logically identical, but clearly they can't be. I looked over the official Python reference but it only added to confusion. Could someone shed a light on this?