Membership Operators in Python

Introduction

The membership operator of python checks for the membership of the given value in given range. Python provides the two in-built membership operators:

Identity operatorMeaning
inReturns True if the value is present in given sequence otherwise returns False.
not inReturns True if the value is not present in given sequence otherwise returns False.

 

Examples

Example 1:

x = [10,9,8,7,6,5]

y = 5

 

if y in x:

print(“Present”)

else:

print(“Not present”)

 

Output:

Present

Example 2:

 

x = [10,9,8,7,6]

y = 5

 

if y not in x:

print(“Not Present”)

else:

print(“Present”)

 

Output:

Not Present

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.