Set Data Type 

 A set is a mutable data type with nonduplicate, unordered values, providing the usual mathematical set operations as shown in Figure 

Creating a set: To create a set, you can use the set() function or use curly braces {}. 

my_set = set() 
print(my_set)  # Output: set() 
another_set = {1, 2, 3} 
print(another_set)  # Output: {1, 2, 3}

However, be careful when using curly braces for an empty set, as it may create an empty dictionary ({}) in certain cases. To ensure you're creating an empty set, it's safer to use the set() constructor.  

Sets do not have duplicate elements, adding an already existing item to a set result in no change to the set.  

set constructor can be used to initialize a set with the elements from a list, tuple, or any other iterable. 

in operator 

One of the most commonly used set operators is the in operator (which we have been already using with sequences) for determining membership, 

  • The items in the set are not displayed in the order that they were defined.  
  • Sets, like dictionaries, do not maintain a logical ordering.  
  • The order that items are stored is determined by Python, and not by the order in which they were provided.  
  • Therefore, cannot access an element of a set by index value.  

Add And Remove Methods 

The add and remove methods allow sets to be dynamically altered during program execution, as shown below, 

Types 

There are two set types in Python— 

  • The mutable set type, and  
  • The immutable frozenset type.  

The immutable frozenset type.  

  • In Python, a `frozenset` is an immutable version of a set.  
  • Once a `frozenset` is created, its elements cannot be modified.  
  • Methods add and remove are not allowed on sets of frozenset type.  
  • Thus, all its members are declared when it is defined, 
  • A frozenset type is needed when a set is used as a key value in a given dictionary. 

To create a `frozenset`, you can pass an iterable (e.g., a list) to the `frozenset()` function. The elements of the `frozenset` are derived from the elements of the iterable. 

Here's an example that demonstrates creating a `frozenset` using a list: 

my_list = [1, 2, 3, 4, 5] 
my_frozenset = frozenset(my_list) 
print(my_frozenset)  # Output: frozenset({1, 2, 3, 4, 5})