I know that if I want to get the intersection of two sets (or frozensets) I should use the ampersand &. Out of curiosity I tried to use the word ‘and’
a = set([1,2,3])
b = set([3,4,5])
print(a and b) #prints set([3,4,5])
I am just curious why? what does this and represent when used with lists?
x and yjust treats the wholexandyexpressions as boolean values. Ifxis false, it returnsx. Otherwise, it returnsy. See the docs for details.Both
sets (as in your example) andlists (as in your question) are false if and only if they’re empty. Again, see the docs for details.So,
x and ywill returnxif it’s empty, andyotherwise.