In our experience, Python developers have tried to create sets in Python that are similar to mathematical sets to make them easier to understand. This is beneficial if you have studied sets before.

A set is an unordered collection of elements, defined by curly braces {}, with elements separated by commas (,).

Sets are “mutable” data types. H. Can be changed after creation.

**Example**

mySet = {1, 2, 3, 4, 5} yourSet = {"This", "is", "your", "set"} print(mySet) print(yourSet)

**Output**

{1, 2, 3, 4, 5} {'is', 'set', 'your', 'This'}

A set contains individual elements. H. There are no duplicate elements.

You can freely declare sets with duplicate elements, but Python will only count them once without error.

**Example**

mySet = {1, 2, 3, 4, 5, 1, 2, 3} print(mySet)

**Output**

{1, 2, 3, 4, 5}

Sets are unordered, so you cannot know the index of the elements in advance. H. Elements can have different index values or the same index value at different times.

**Example**

mySet = {1, 2, 3, 4, 5} yourSet = {"This", "is", "your", "set"} print(mySet) print(yourSet)

**Output(different times)**

{1, 2, 3, 4, 5} {'set', 'is', 'your', 'This'} {1, 2, 3, 4, 5} {'This', 'is', 'set', 'your'} {1, 2, 3, 4, 5} {'set', 'is', 'This', 'your'}

## Check an element in the set

You can check if an element exists in a set. You can use classic methods such as for loops and while loops to run until an element no longer matches any element in the set. However, here we use the “in” keyword to check for the presence of an element in the set.

### Example

mySet = {1, 2, 3, 4, 5} print(1 in mySet) # will return True if present else False print(5 in mySet) print(6 in mySet) print(100 in mySet)

### Output

True True False False

## Updating sets

You can add more elements to the set using “add()” and “update()”.

“add()” is used to add one element and “update()” is used to add any number of elements.

Example

mySet = {1, 2, 3, 4, 5} mySet.add(6) print(mySet) mySet.update([7, 8, 9]) print(mySet)

### Output

{1, 2, 3, 4, 5, 6} {1, 2, 3, 4, 5, 6, 7, 8, 9}

## set() method

We can use **“set()”** method to create sets.

### Example

myString = "I am a String" myList = [1, 2, 3, 4] myTuple = (5, 6, 7, 8) set1 = set(myString) set2 = set(myList) set3 = set(myTuple) print(set1) print(set2) print(set3)

### Output

{'r', 'i', 'n', 'S', 'g', ' ', 'I', 'a', 'm', 't'} {1, 2, 3, 4} {8, 5, 6, 7}

## len()

Using **“len()”**, we can find out the length of sets i.e. the number of elements present in a set.

### Example

set1 = {1, 2, 3, 4, 5} set2 = {"I", "am", "a", "set"} print(len(set1)) print(len(set2))

### Output

5 4

## clear() and del

Use **“clear()”** to clear the elements of a set i.e. to delete the elements inside a set.

Use **“del”** keyword to delete a set permanently.

### Example

mySet = {1, 2, 3, 4, 5} mySet.clear() print(mySet) del mySet print(mySet)

### Output

set() Traceback (most recent call last): File "f:/vscode/python/fsggr.py", line 5, in <module> print(mySet) NameError: name 'mySet' is not defined

## remove() and discard()

To remove elements from a set, use remove() and Discard().

‘remove()’ will throw an error if the element to be removed is not present in the set.

‘discard()’ does not throw an error if the element is not present in the set.

### Example

mySet = {1, 2, 3, 4, 5} mySet.remove(1) print(mySet) mySet.discard(2) print(mySet)

### Output

{2, 3, 4, 5} {3, 4, 5}

## pop() method

You can remove the last element from a set using the Pop() method. However, we know that sets are unordered, so we cannot know which elements will be removed from the set. “pop()” returns the value removed from the set using the pop() method.

### Example

mySet = {1, 2, 3, 4, 5} returnValue = mySet.pop() print(mySet) print(returnValue)

### Output

{2, 3, 4, 5} 1

## Useful Methods

Method | Description |
---|---|

union() | Return a set containing the union of sets |

copy() | Returns a copy of the set |

difference() | Returns a set containing the difference between two or more sets |

intersection() | Returns a set, that is the intersection of two other sets |

isdisjoint() | Returns True if two sets contain similar elements |

issubset() | Returns True if the set is contained in the set |

issuperset() | Returns True if the set contains another set |

symmetric_difference() | Returns a set with the symmetric differences of two sets |

** PREVIOUS– PYTHON BOOLEAN ****NEXT– pYTHON DICTIONARY **