7. set in python

set is an unordered collection of unique items. Set is defined by values separated by comma inside braces.

In set:

  1. Items in a set are not in ordered.

  2. Since elements are not in ordered so we can access or change the elements of set using indexing or slicing.

  3. Instead of that we have set operation such as union, interesection, different on two sets etc.

  4. set eliminates duplicates.


s1={1,2,3}

print(s1)

s2={1,2,3, 2,1,2}

print(s2)


Set built in function :

  1. len(set)

  2. max(set)

  3. min(set)

  4. sum(set)

  5. sorted(set)

  6. enumerated(set)

  7. any(set)

  8. all(set)

  9. set.add(obj)

  10. set.remove(obj)

  11. set.discard(obj)

  12. set.pop()

  13. set1.union(set2)

  14. set1.update(set2)

  15. set1.intersection(set2)

  16. set1.difference(set2)