A set is a collection which is unordered and unindexed and it is written within curly brackets.
We can’t access items within sets using indexes since it is unordered. We need to loop through all the items to retrieve the items.
Example:

Output:

The above example shows that the set named “firstSet” is created and looped through the items inside the firstSet set so that it is printing the items from the set.
Check if an item is present in Set
Example:

In the last line, the code is checking whether “Num2” is present in the set firstSet.
The output would return boolean value either true or false.
Since, “Num2” is present in the firstSet, it should return true.

Addition of Items in Sets:
Addition of items always add the item to the end of the collection.
Example:

Output:

Removal of Items:
Removal of items will remove the item at the end of the collection.
Example:

Output:

Length:
To determine the number of items in set, we use length keyword.
Example:

In the above code, I am retrieving the length of the set and then converting the value to string to print the value.
Output:

The length of the set is 3 since the firstSet has only 3 items now.
Union: This is a very important method in Set where it combines the values from two sets and returns one single set.
Example:

In the above code, i have created two sets, set1 and set2.
In the last line, sets are combined using union. Let’s see what the output is to understand better.
Output:

As shown in the output, the union operator combines the items from both the sets and displays as one set.