Can't understand set operations in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can't understand set operations in python

def union(a, b): return print(a|b) union(10,20) #output = 30 union(2,3) #output = 3 def intersection(a, b): return print(a&b) intersection(10,20) #output = 0 intersection(2,3) #output = 2 How????? Please, can anyone explain it?

12th Mar 2018, 1:33 PM
Dinesh Banjara
Dinesh Banjara - avatar
1 Answer
0
Your program actually performs 'bitwise' operation AND (&) and OR (|) between numbers, not 'set' operation. Let's print the arguments and return values with binary format to see it clearly (e.g: print("my_int = ", "{0:08b}".format(my_int))) To do 'set' operation, the input arguments need to be 'set' type, try using these variables instead and see the differences: a = {10, 20} b = {20, 30}
12th Mar 2018, 2:13 PM
tin le