Sets in Python

 What is Sets?

Sets are an unordered collection of unique elements and created by using set() function. 


-- Creating a Set

my_set=set()

-- Adding an element to set using add() method

>>> myset=set()
>>> myset.add(5)
>>> myset
{5}

--- Lets try to add additional values to set

>>> myset.add(6)
>>> myset.add(7)
>>> myset
{5, 6, 7}

--- Lets try to add duplicate values into the set 

>>> myset
{5, 6, 7}
>>> myset.add(7)   ------------ adding the same item "7" which is already present in the Set
>>> myset
{5, 6, 7}        -------------------- It did not add the items "7" twice but it just maintain uniqueness 


--- Let try to get the unique values from the set

>>> myset=[9,9,9,8,8,8,7,7,7]
>>> set(myset)
{8, 9, 7}

>>> set('Administration')
{'a', 'd', 'A', 'r', 's', 'o', 'm', 't', 'n', 'i'}

Comments

Popular posts from this blog

Tuples in Python

Auditing in Oracle database

Boolean in Python