Membership operator in Python

Python Membership operator

Python membership operator are used to check, whether a sequence or values are present in an object.


For example, to check whether 'h' is present in the string 'Hello world', we make use of 'in' operator.


OperatorOperation
inReturns True if a sequence/value 
is present in an object
not inReturns True if a sequence/value 
is not present in an object

Python 'in' and 'not in' membership operator

In Python, to check if a value or a sequence is present in an object, we make use of 'in' membership operator. 


And 'not in' to find if a sequence/value is not present in an object.


Program

>>> st = "Hello world!"
>>> 'H' in st
True
>>> 'world' in st
True
>>> "Sleep" in st
False

>>> "Sleep" not in st
True
>>> 'Hello' not in st
False





Conclusion

Python membership operator are used to check, whether a sequence or values are present in an object..

Returns True if value is present in a sequence, while working with 'in' operator.





Previous                          Next

If you have any query, feel free to ask.

Post a Comment

If you have any query, feel free to ask.

Post a Comment (0)