Set
instance of set class. unordered sequence of elements. contain unique elements. it is mutable object. usually contain same data type. elements have no index.
creating a set
use {} to create a set. you cannot add mutable objects (list, dict) to the set. only immutable objects (str, int, float, tuple, frozenset) can be added to the set. not allowed to create empty set using {}, you have to use set().
get length
use len()
access element
As set element do not have index, you can not access element by index. but you can use in keyword to check if element is present in the set.
set theory
todo
set methods
there are many methods.
add()- add element to the setunion()- return the union of two setsupdate()- update the set with the union of itself and othersremove()- remove element from the set, produce error if element not presentdiscard()- remove element from the set if it is presentdifference()- return the difference of two setsintersection()- return the intersection of two setsissubset()- test whether every element in the set is in another setissuperset()- test whether every element in another set is in the setpop()- remove and return an arbitrary set elementclear()- remove all elements from the setcopy()- return a copy of the set