## Sets

- Sets are similar to lists but with a difference that sets are lists with no duplicate entries.
- set is a mutable and an unordered collection of items. Means we can easily add or remove items from it.
- A set is created by placing all the elements inside curly brackets { }, seperated by comma or by using the built-in function
*set()*.

```
Syntax: set_variable = {val1, val2, ... }
Example:
1.S={1,2.0.”abc”}
2.set([1,2.0,”abc”])
```

*A set can have any number of items and they may be of different data types.*

**SET OPERATIONS**

Note:

- Two sets are equal if and only if every element in each set is contained in the other.
- A set is less than another set if and only if the first set is a subset of the second set.
- A set is greater than another set if and only if the first set is a superset of the second set.

S.NO | OPERATION | USAGE | EXAMPLE |

1 | update | Adds elements of one set to the other sets provided that all duplicates are avoided. Syntax: set1.update(set2) | s=set([1,2,3,4,5]) t=set([6,7,8]) s.update(t) print(s) #prints {1, 2,3,4, 5, 6, 7, 8} |

2 | add | Adds an element to the set provided that all duplicates are avoided Syntax:set.add(element) | s=set([1,2,3,4,5]) s.add(6) print(s)#prints {1, 2, 3, 4, 5, 6} |

3 | remove | Removes an element from the set. Returns KeyError if the element is not present Syntax:set.remove(element) | s=set([1,2,3,4,5]) s.remove(2) print(s)#prints {1, 3, 4, 5} |

4 | discard | Same as remove() but does not give an error if the element is not present in the set Syntax:set.discard(element) | s=set([1,2,3,4,5]) s.discard(2) print(s)#prints {1, 3, 4, 5} |

5 | pop | Removes and returns an arbitrary element from a set.KeyError is raised if a set is empty. Syntax:set.pop() | s=set([1,2,3,4,5]) s.pop()#returns 1 print(s)#prints {2, 3, 4, 5} |

6 | clear | Removes all elements from the set. Syntax:set.clear() | s=set([1,2,3,4,5]) s.clear() print(s)#prints set() |

7 | len | Returns the length of the set Syntax: len(set) | s=set([1,2,3,4,5]) p=len(s) print(p)#prints 5 |

8 | in | Returns True if the element is present in set and False otherwise Syntax: element in set | s=set([1,2,3,4,5]) p=3 in s print(p) #prints True |

9 | not in | Returns True if the element is not present in set and False otherwise Syntax: element not in set | s=set([1,2,3,4,5]) p=6 not in s print(p) #prints True |

10 | issubset | Returns True if every element in set s is present in set t and False otherwise Syntax: s.issubset(t) or s<=t | s=set([1,2,3,4,5]) p=set([1,2,3,4,5,6,7,8]) print(s.issubset(p)) #prints True print(s<=p) #prints True |

11 | issuperset | Returns True if every element in t is present in set t and False otherwise Syntax: s.issuperset(t) or s>=t | s=set([1,2,3,4,5]) p=set([1,2,3,4,5,6,7,8]) print(s.issuperset(p)) #prints False print(s>=p) #prints False |

12 | union | Returns a set s that has elements from both sets s and t Syntax: s.union(t) or s|t | s=set([1,2,3,4,5]) p=set([1,2,3,4,5,6,7,8]) print(s.union(p))#prints {1, 2, 3, 4, 5, 6, 7,8} print(s|p)#prints {1, 2, 3, 4, 5, 6, 7, 8} |

13 | intersection | Returns a new set that has elements that are common to both sets s and t Syntax: s.intersection(t) or s&t | s=set([1,2,3,4,5]) p=set([1,2,3,4,5,6,7,8]) print(s.intersection(p))#prints {1, 2, 3, 4, 5} print(s&p)#prints {1, 2, 3, 4, 5} |

14 | intersection_update | Returns a set that has elements that are common to both the sets s and t Syntax: s.intersection_update(t) | s=set([1,2,3,4,5]) p=set([1,2,3,4,5,6,7,8]) s.intersection_update(t) print(s)#prints {1, 2, 3, 4, 5, 6, 7,8} |

15 | difference | Returns a new set that has elements in set s but not in t. Syntax: s.difference(t) or s-t | s=set([1,2,3,4,5]) p=set([1,2,3,4,5,6,7,8]) print(s.difference(p)) #prints set() print(s-t)#prints set() |

16 | difference_update | Removes all elements of another set from this set Syntax: s.difference_update(t) | s=set([1,2,3,4,5]) p=set([1,2,3,4,5,6,7,8]) s.difference_update(p) print(s) #prints set() |

17 | symmetric difference | Returns a new set with elements either in s or in t but not both. Syntax: s.symmentric_difference(t) ors^t | s=set([1,2,3,4,5]) p=set([1,2,3,4,5,6,7,8]) print(s.symmetric_difference(p))#prints {6, 7,8} print(s^t)#prints {6, 7,8} |

18 | copy | Returns a copy of set s Syntax: set.copy() | s=set([1,2,3,4,5]) p=s.copy() print(p) #prints {1, 2, 3, 4, 5} |

19 | isdisjoint | Returns True if two sets have a null intersection. Syntax: s.isdisjoint(t) | s=set([1,2,3,4,5]) p=set([1,2,3,4,5,6,7,8]) print(s.isdisjoint(p)) #prints False |

20 | equals(==) | returns True if the two set are equivalent and False otherwise Syntax:s==t | s=set([1,2,3,4,5]) p=set([1,2,3,4,5,6,7,8]) print(s==p) #prints False |

21 | Not equals(!=) | returns True if both sets are not equivalent and False otherwise Syntax: s!=t | s=set([1,2,3,4,5]) p=set([1,2,3,4,5,6,7,8]) print(s!=p) #prints True |

22 | all | Returns True if all elements in the set are True otherwise False. Syntax: all(s) | s=set([1,2,3,4,5]) print(all(s)) #prints True |

23 | any | Returns True if any of the elements in the set is True. Returns False if the set is empty. Syntax: any(s) | s=set([1,2,3,4,5]) print(all(s)) #prints True |

24 | enumerate | Returns an enumerate object which contains the index as well as the value of all the items of the set as a pair. Syntax:enumerate(s) | s=set([1,2,3,4,5]) for i in enumerate(s): print(i,end=’ ‘) prints (0, 1) (1, 2) (2, 3) (3, 4) (4, 5) |

25 | max | Returns the maximum value in a set Syntax: max(set) | s=set([1,2,3,4,5]) print(max(s))#prints 5 |

26 | min | Returns the minimum value in a set Syntax: min(set) | s=set([1,2,3,4,5]) print(min(s))#prints 1 |

27 | sum | Returns the sum of elements in a set Syntax: sum(s) | s=set([1,2,3,4,5]) print(sum(s))#prints 15 |

28 | sorted | Returns a new sorted list from elements in the set. It does not sort the set as set are immutable. Syntax:sorted(s) | s=set([5,4,3,2,1]) print(sorted(s))#prints {1,2,3,4,5} |