← Back to Python Course | Chapter 2: Input, Output & Operators | Lesson 15 of 16

Python Membership Operators

Membership operators जाँचते हैं कि कोई चीज़ किसी समूह के अंदर है या नहीं, जैसे यह देखना कि कोई खिलौना खिलौनों के बक्से में है या नहीं। in और not in आपको True या False देते हैं।
Syntax
python
item in collection
item not in collection

Membership Operators क्या हैं?

Membership operators जाँचते हैं कि कोई value list, tuple, string, set या dictionary जैसे container के अंदर मौजूद है या नहीं, और हाथ से loop चलाए बिना सादा True या False लौटाते हैं।

ये लगभग अंग्रेज़ी की तरह पढ़े जाते हैं, जो एक कारण है कि membership जाँचने वाला Python code इतना संक्षिप्त और पठनीय होता है।

उदाहरण: What Are Membership Operators?

python
fruits = ["apple", "banana"]
print("apple" in fruits)

in और not in

in operator True लौटाता है अगर बाईं ओर का value दाईं ओर के container में मिल जाए, और not in उसका ठीक उल्टा है।

दोनों हर built-in sequence और collection type पर काम करते हैं, जो इन्हें भाषा के सबसे व्यापक रूप से पुन: उपयोग होने वाले operators में से एक बनाता है।

उदाहरण: in and not in

python
fruits = ["apple", "banana"]
print("apple" in fruits)  # True, value is present
print("kiwi" not in fruits)  # True, value is absent

Strings में Membership

Strings के लिए, in केवल अलग-अलग characters की जाँच करने के बजाय substring खोज करता है, इसलिए cat in concatenate True लौटाता है क्योंकि अक्षरों का वही सटीक क्रम बड़ी string के भीतर मौजूद है।

यह अलग method बुलाए बिना substrings जाँचने का त्वरित तरीका है।

उदाहरण: Membership in Strings

python
print("cat" in "concatenate")

Dictionaries में Membership

Dictionary पर इस्तेमाल होने पर, in डिफ़ॉल्ट रूप से dictionary की keys के विरुद्ध जाँच करता है, values के नहीं — name in my_dict जाँचता है कि name key है या नहीं, इसलिए किसी value की जाँच के लिए in my_dict.values() साफ़ लिखना पड़ता है।

यह उन शुरुआती लोगों को उलझाता है जो उम्मीद करते हैं कि यह values खोजेगा।

उदाहरण: Membership in Dictionaries

python
user = {"name": "Alex", "age": 30}
print("name" in user)  # checks the dict's keys by default
print("Alex" in user.values())  # must check .values() explicitly for values

in का __contains__ से संबंध

भीतर से, in container का __contains__ method बुलाता है अगर वह परिभाषित हो, इसीलिए custom classes उस dunder method को खुद लागू करके membership testing support कर सकती हैं।

जिन objects में __contains__ नहीं होता, उनके लिए Python हर element पर iterate करके तुलना करने पर लौट आती है।

उदाहरण: How in Connects to __contains__

python
class MyBox:
    def __init__(self, items):
        self.items = items
    def __contains__(self, item):  # lets "in" work on instances of this class
        return item in self.items

box = MyBox([1, 2, 3])
print(2 in box)  # calls MyBox.__contains__ under the hood
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.