0

We have: (set1,set2)

why does print(set1 or set2) return set1 instead of (set1 | set2) and print(set1 and set2) return set2 instead of set1.intersection(set2) ?

Codi
  • 57
  • 8
  • 1
    @ThierryLathuille I think the duplicate is not quite right. OP asks why there is a difference between `and` and `&`, and `or` and `|`. They don't ask why `or` and `and` return a non boolean value – DeepSpace Oct 30 '21 at 13:23
  • 1
    `and` is the boolean operand, equivalent to `&&` in other languages you might be familiar with. `&` is the bitwise `and` in Python, which in sets is wired to call the `set.intersection` method (actually, it is the other way around but the idea stands). Same goes for `or`, `|` and `union`. – DeepSpace Oct 30 '21 at 13:26
  • @DeepSpace Right, there are two parts to the question, I added a second duplicate about `&` and `and`. – Thierry Lathuille Oct 30 '21 at 13:26

1 Answers1

0

That is how boolean operand works

  • or

    • if left operand is True/Truthy => return it
    • if left operand is False/Falthy => return right operand
  • and

    • if left operand is False/Falthy => return it
    • if left operand is True/Truthy => return right operand
azro
  • 53,056
  • 7
  • 34
  • 70