Python set len ,set in and set not in

In this post we will discuss the Python set len() function,the set in and set not in keywords.

i)Set len()
ii)Set in
iii)Set not in


i)len()

The set len() function is used to get the number of elements in the set.This function accept one argument.

The implementation of len() is simple.Call the len() and pass the name of the set object whose number of elements you want to determine.

>>> st1={12 ,34, 'text' , .900}
>>> st2={'new' ,784, 'm'}
>>> len( st1 )
4
>>> len(st2)
3

ii)The set ‘in’

The set in keyword allows you to test if an element is present in the set.The foramt of using ‘in’ is:
 
element in set
 
element is the element to check if it is present in the set

Consider the example below.

>>> st={ 'new' , 'tExt' , '0.674' , 'C++' , 900}
>>> 'Text' in st
False
>>> 0.674 in st
False
>>> 900 in st
True


ii)The set ‘in’

The set in keyword aloows you to test if an element is present in the set.The foramt of using ‘in’ is:
 
element in set
 
element is the element to check if it is present in the set

Consider the example below.

>>> st={ 'new' , 'tExt' , '0.674' , 'C++' , 900}
>>> 'Text' in st
False
>>> 0.674 in st
False
>>> 900 in st
True

‘tExt’ is present in st not ‘Text’ hence the first code returns false.’0.674′ is present in st not 0.674 hence the second code also returns false.The third one returns true since 900 is present in st.


ii)The set ‘not in’

The set ‘not in‘ is simialr to ‘in‘ except not in checks if the element is not present in the set.

The format of using not in is similar to ‘in’.

>>> st={ 'new' , 'tExt' , '0.674' , 'C++' , 900}
>>> 'Text' not in st
True
>>> 0.674 not in st
True
>>> 'C++' not in st
False