In the above syntax, “it” is an iterable object whose all items or elements are inserted into the newly created set. If the passed arguments are null, empty, or not defined, it will create a new empty set.
There are different functionalities associated with the set() method. Some of them are listed below:
Here, “values” are those elements that are going to be added to the set.
To do so, declare a set using “Set()” constructor:
Utilize the “set.add()” method to add the element in a set. This method will add the elements in a sequence and skip the elements that are duplicated:
Display the output on the console with the help of the “log()” method and pass the set as the argument:
It can be observed that the specified elements have been added in the set successfully:
In the above syntax, “val” defines the element which will be removed or deleted from the set.
Now, first, initialize the variable and pass the value within the “Set()” constructor. Then, pass the value as the parameter of the set() method:
Invoke the log() method and pass the set as an argument to print it set on the console:
Now, use the “set.delete()” method as the argument of the “log()” method and pass the values that need to be removed:
As you can see that the defined values have been deleted from the set:
First, initialize the variable and utilize the “Set()” method and pass the values:
Utilize the “set.clear()” method to clear the set:
Lastly, display the output on the screen by using the “log()”:
It can be noticed that the set size is now “0”: