DS160123 Python Set Operations

A = {1,3,5}

B = {1,2,3}

C = {2,3,6}

B-C-A ?

Output : Set()

Why?

@ritheshchandra1707

Are you using the print statement in your code or not?

1 Like

B-C-A is ambiguous, as it could be telling perform B-C first and then take difference with A i.e.
(B-C)-A or B-(C-A)

Why does it matter??
Take A={1,2,3,4} , B={4,5,6,7}, C={7,8,9,10}
Now,

  1. (B-C) = {4,5,6} i.e.What {4,5,6,7} have that {7,8,9,10} doesnt have
    (B-C)-A = {5,6} i.e. What {4,5,6} have that {1,2,3,4} doesnt have

If you solve, B-(C-A) like this, you will get B-(C-A) = {4,5,6}
As we can see, both equations give different answers but looks same without brackets.
So, without brackets python doesn’t know which meaning to take, thus it only gives empty set as output.

1 Like