Membership Operators in Python

Table of Contents

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 operator Meaning
in Returns True if the value is present in given sequence otherwise returns False.
not in Returns 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

Author

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

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