MEMBERSHIP OPERATOR IN PYTHON

Python Free Tutorial

Python’s membership operators test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary). If the value is present in the data structure, then the resulting value is true otherwise it returns false. These membership operator in python are an example of what makes Python so easy to use compared to lower-level languages such as C.

in         True if value is found in the sequence
not in     True if value is not found in the sequence
Example 1:
>>> print (2 in {2,3,4})
>>> print( 'e' in ('aeiou'))

output:
True
True
Example 02:
# In a dictionary we can only test for presence of key, not the value
>>> a = { "s":"sawid",
        "d":"diwas"}
>>> print ("s" in a)
>>> print ("sawid" not in a)

OUTPUT:
True
True

This page is contributed by Diwas & Sunil . If you like AIHUB and would like to contribute, you can also write an article & mail your article to  itsaihub@gmail.com . See your articles appearing on AI HUB platform and help other AI Enthusiast.

About Diwas

🚀 I'm Diwas Pandey, a Computer Engineer with an unyielding passion for Artificial Intelligence, currently pursuing a Master's in Computer Science at Washington State University, USA. As a dedicated blogger at AIHUBPROJECTS.COM, I share insights into the cutting-edge developments in AI, and as a Freelancer, I leverage my technical expertise to craft innovative solutions. Join me in bridging the gap between technology and healthcare as we shape a brighter future together! 🌍🤖🔬

View all posts by Diwas →

One Comment on “MEMBERSHIP OPERATOR IN PYTHON”

Leave a Reply

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