Python Set

Python Set Introduction:

In Python, a set is an unordered collection of unique elements. Sets are mutable and can be modified using various methods.

To create a set in Python, you can use curly braces `{}` or the built-in `set()` function. Here’s an example.

  1. Uniqueness: Sets are collections of unique elements. Each element appears only once in a set. If you try to add a duplicate element, it will be ignored.
  2. Mutable: Sets are mutable, meaning you can modify them by adding or removing elements after they are created.
  3. Unordered: Sets are unordered collections, which means the elements are not stored in any particular order. You cannot access elements by indexing or slicing.
  4. Creation: Sets can be created using curly braces `{}` or the built-in `set()` function. For example.

5. Membership Testing: Sets provide an efficient way to test if an element exists in a set using the `in` operator. This operation has a constant-time complexity compared to lists or tuples.

6. Iteration: You can iterate over the elements of a set using a `for` loop, which will visit each element in an arbitrary order.


In Python, you can delete a set using the `del` keyword. Here’s an example of how to delete a set. Here’s an example:

In the example above, the `del` keyword is used to delete the `my_set` variable, which contains the set. After deleting the set, any attempt to access or use the set will result in a `NameError` because the set no longer exists.


  1. Union: The union of two sets `A` and `B` contains all unique elements from both sets. In Python, you can perform the union operation using the `union()` method or the `|` operator.
  1. Intersection: The intersection of two sets `A` and `B` contains only the elements that are common to both sets. In Python, you can perform the intersection operation using the `intersection()` method or the `&` operator.
  1. Difference: The difference between two sets `A` and `B` contains the elements that are in `A` but not in `B`. In Python, you can perform the difference operation using the `difference()` method or the `-` operator.
  1. Symmetric Difference: The symmetric difference of two sets `A` and `B` contains the elements that are in either `A` or `B`, but not both. In Python, you can perform the symmetric difference operation using the `symmetric_difference()` method or the `^` operator.
  1. len(): The `len()` function returns the number of elements in a set.
  1. add(): The `add()` method adds an element to a set.
  1. remove(): The `remove()` method removes an element from a set. It raises a KeyError if the element does not exist in the set.
  1. discard(): The `discard()` method removes an element from a set if it exists. It does not raise an error if the element is not found.
  1. pop(): The `pop()` method removes and returns an arbitrary element from the set. Since sets are unordered, the popped element is not guaranteed to be the first or last element.
  1. clear(): The `clear()`method removes all elements from a set, making it empty.
  1. copy(): The `copy()` method creates a shallow copy of a set, allowing you to work with a separate copy of the original set.

Leave a Comment