The elements of the set are not repeated and unique .
Set creation :
1. Use {} Create a collection object
>>> a = {3,5,7}
>>> a
{3, 5, 7}
2. Use set(), Will list 、 Turn iteratable objects such as tuples into collections .
Be careful : De duplication if there are duplicate elements .
>>> a = ['a','b','c','b']
>>> b = set(a)
>>> b
{'b', 'a', 'c'}
Addition of collection elements :
add() add to
>>> a=[3,5,7]
>>> a.add(9)
>>> a
{9, 3, 5, 7}
Deletion of collection elements :
1.remove() Deletes the specified element
>>> a = {10,20,30,40,50}
>>> a.remove(20)
>>> a
{10, 50, 30}
2.clear() Empty
>>> a={1,2,3,4,5}
>>> a.clear()
>>> a
{}
Set related operations :
>>> a = {1,3,'sxt'}
>>> b = {'he','it','sxt'}
1. intersection
>>> a&b
{'sxt'}
>>> a.intersection(b)
{'sxt'}
2. Combine
>>> a|b
{1, 3, 'sxt', 'he','it'}
>>> a.union(b)
{1, 3, 'sxt', 'he','it'}
3. Difference set
>>> a-b
{1, 3}
>>> a.difference(b)
{1, 3}
>>> b-a
{'it', 'he'}