Adding elements to a Set:
Elements can be added to a set using two functions, the add() and the update() function.
The add() function adds one element to the existing set as shown below:
Example:
1
2
3 |
My_Set={1,'s',7.8}
My_Set.add(3)
My_Set |
Output: {1, 3, 7.8, ‘s’}
The update() function is used when you want to add more than one element to the existing set.
Example:
1
2
3 |
My_Set={1,'s',7.8}
My_Set.update([2,4.6,1,'r'])
My_Set |
Output: {1, 2, 4.6, 7.8, ‘r’, ‘s’}
As you can see in the above output, the update() function is taking a list of 4 values and all values except 1 are added to My_Set. This is because 1 is already present in the set and therefore, it cannot be added again.