In [2]: a = set () # Common Operation 1In [3]: Aout[3]: Set () in [4]: Type (a) out[4]: SetIn [5]: b = Set ([1, 3]) in [6]: Bout[6]: {1, 3 }in [7]: type (b) out[7]: setIn [8]: B.update (2)--------------------------------------------------------------------- ------TypeError Traceback (most recent) <ipython-input-8-d51e2fe4c50a> in <module> ()---- > 1 b.update (2) TypeError: ' int ' object is not Iterablein [9]: B.update ({2}) in [ten]: bout[10]: {1, 2, 3}in [all]: B.updat E ([4]) in []: bout[12]: {1, 2, 3, 4}in []: a.dia.difference a.difference_update A.discardin [13]: A.difa.difference A.difference_updatein []: a.difference (b) out[13]: Set () in [+]: aout[14]: Set () in [15]: B.difference (a) out[15]: {1, 2, 3, 4}in [16]:
Common Operations 2
in [+]: A.add ({1, 3})---------------------------------------------------------------------------TypeError Traceback (most recent) <ipython-input-16-98cdf4d0875e> in <module> ()----> 1 a.ad D ({1, 3}) typeerror:unhashable type: ' Set ' in [+]: A.add (4) in []: aout[18]: {4}in []: A.issua.issubset A.issuperset in [+]: A.issubset (b) out[19]: Truein [4]: A.remove () in [Max]: aout[21]: Set () in [All]: A.union (b) out[22]: {1, 2, 3, 4}in [aout[23]: Set () in [[]: Bout[24]: {1, 2, 3, 4}in [+]: B.pop () out[25]: 1In []: a.copy (b)-------------------------- -------------------------------------------------TypeError Traceback (most recent call Las T) <ipython-input-26-9e8a5f057ffd> in <module> ()----> 1 a.copy (b) typeerror:copy () takes no arguments (1 given) in [+]: a.copy () out[27]: Set () in [+]: c = a.copy () in [Max]: cout[29]: Set () in [d]: aout[30]: Set () in [+]: A.add ({ 234})---------------------------------------------------------------------------TypeError Traceback (most recent call Last) <ipython-input-31-6073e02d68a9> in <module> ()----> 1 A.add ({234}) typeerror:unhashable type: ' Set ' in [+]: A.add (234) in [to]: cout[33]: Set () in []: aout[34]: {234}
Common Operations 3
in [+]: A.clear () in [approx]: aout[36]: Set () in []: a = {1}in [+]: b = {1, 2}in [in]: A.intersection (b) out[41]: {1}in [43] : aout[43]: {1}in []: b = {1, 2, 3}in []: a.union (b) out[45]: {1, 2, 3}in []: a.union (b) out[45]: {1, 2, 3}in []: A & bout[46]: {1}in [+]: a ^ bout[47]: {2, 3}in []: a-bout[48]: Set () in [further]: b-aout[49]: {2, 3}
in [[]: a > Bout[50]: Falsein [Wuyi]: B > aout[51]: Truein [[]: A = = bout[52]: Falsein [in]: A! = bout[53]: True
In Python, set uses the