Python‎ > ‎operator‎ > ‎

operator.contains()

http://www.adilwaseem.com/

Return 
Return with bool value True or False whether b contains in a.

Syntax
operator.contains()->bool
__contain__

Parameters
Number or string

Import module
Importing module is required in the Shell window and the Editor window.

Examples
The example of method is:
>>>import operator
 >>> a="Python"
>>> b="Lake"
>>> operator.contains(a,b)
False
>>> b="Python"
>>> operator.contains(a,b)
True
>>>

Method(b in a)
 The method(b in a) returns the same value.
 >>> x=["Python","Lake"]
>>> y=["Python","Lake"]
>>> operator.contains(x,y)
False
>>> x in y
False
>>> y in x
False
>>> a="Python"
>>> b="Lake"
>>> b in a
True
>>>

operator.__contains__(a, b)
The method returns the same value as operator.contains(a, b).
>>> a="Python"
>>> b="Lake" 
>>> operator.__contains__(a, b)
True
>>>

__contains__
The method tests whether the item contain in set.
 >>> set1={4,5,6,7}
>>> set2={6,7,8,9,10}
>>> set1.__contains__(5)
True

Limitation
Even though the x and y is equal but it returns with false.
 >>> x=(1,2,3,4,5)
>>> y=(2,3)
>>> operator.contains(x,y)
False
>>> operator.contains(y,x)
False
>>> x=(1,2,3,4,5)
>>> y=(1,2,3,4,5)
>>> operator.contains(x,y)
False
>>> x=["Python","Lake"]
>>> y=["Python","Lake"]
>>> operator.contains(x,y)
False
>>> 



Comments