What are sets in Python

Sets in python is the collection of unordered items. These items are unindexed also. In Python sets are surrounded by curly brackets.

Example:-- {23, 45, 67, 90}

Some basic method of Sets :-

1. add() :- This is used to add an item to the Set.

>>> myset = {34, 45, 78}
>>> myset.add('hello')
>>> print(myset)
{34, 'hello', 45, 78}
>>> 

2. update() :- This is used to add multiple items to Set

>>> myset = {34, 45, 78}
>>> myset.update(['a', 'apple', 89, 90])
>>> print(myset)
{'a', 34, 'apple', 45, 78, 89, 90}
>>> 

3. len() :- This is used to get total item of Set.

>>> myset = {34, 45, 78}
>>> print('Total item in myset ', len(myset))
Total item in myset  3
>>> 

4. remove() :- Used to delete an element from set.

>>> myset = {34, 45, 78}
>>> myset.remove(34)
>>> print(myset)
{45, 78}
>>> 

 

Total Views: 413