{"id":7598,"date":"2023-11-02T10:41:06","date_gmt":"2023-11-02T05:11:06","guid":{"rendered":"https:\/\/python-programs.com\/?p=7598"},"modified":"2023-11-10T12:13:22","modified_gmt":"2023-11-10T06:43:22","slug":"the-in-and-not-in-operators-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/the-in-and-not-in-operators-in-python\/","title":{"rendered":"The \u201cin” and \u201cnot in\u201d operators in Python"},"content":{"rendered":"
In this post, we’ll look over Python membership operators, such as in and not in operators. These operators are used to determine whether or not a given data element is part of a sequence and, in the case of the identity operator, whether or not it is of a specific type.<\/p>\n
Python Membership Operators:<\/strong><\/p>\n These operators aid in validating if a particular element exists in or is a member of the provided data sequence. This data sequence can be a list, string, or tuple.<\/p>\n “in” operator(brief):<\/strong><\/p>\n It determines whether or not the value is present in the data sequence. It returns a true value if the element is present in the sequence and a false value if it is not present in the sequence.<\/p>\n “not in ” operator (brief) :<\/strong><\/p>\n This operator examines a sequence for the absence of a value. The out operator is the polar opposite of the in operator. When the element is not found or absent from the sequence, it evaluates to true, and when the element is found in the data sequence, it evaluates to false.<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In Python, the in operator determines if a given value is a constituent element of a sequence such as a string, array, list, or tuple, among other things.<\/p>\n The statement returns a Boolean result that evaluates to True or False when used in a condition. The statement returns True if the supplied value is found within the sequence. When it isn’t found, though, we get a False.<\/p>\n Let us use an example to gain a better grasp of how the in<\/strong> operator works.<\/p>\n 1)Checking if given element is present in list using in operator.<\/b><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n 2)Checking if given element is present in tuple using in operator.<\/b><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n 3)Checking if given element(string) is present in string using in operator.<\/b><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n In Python, the not in operator acts in the exact opposite way as the in operator. It likewise tests for the presence of a specified value within a sequence, but the return values are completely different from those of the in operator.<\/p>\n The statement returns False when used in a condition with the supplied value existing inside the sequence. When it is, we get a False, but when it isn’t, we get a True.<\/p>\n Simply replace the in operator with the not in one in the previous example.<\/p>\n Let us use an example to gain a better grasp of how the not\u00a0 in<\/strong> operator works.<\/p>\n 1)Checking if given element is present in list using not in operator.<\/b><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n 2)Checking if given element is present in tuple using not in operator.<\/b><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n 3)Checking if given element(string) is present in string using in operator.<\/b><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n We previously explored how the in and not in operators function with various types of sequences. Dictionaries, on the other hand, are not sequences. Dictionaries, on the other hand, are indexed using keys.<\/p>\n So, do the above operators work with dictionaries? If they do, how do they respond accordingly?<\/p>\n Let us provide an example to help us understand.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n First, we created a dictionary dictionary with a certain set of random keys and corresponding random values.<\/p>\n As shown in the result above, “100<\/strong>” in dictionary evaluates to False. In contrast, “BTechGeeks<\/strong>” in dict1 yields True.<\/p>\n As a result, it is evident that the in operator searches for the element among the dictionary keys<\/strong> rather than the values.<\/p>\n As previously stated, the not in operator here also evaluates in the same manner. In this post, we’ll look over Python membership operators, such as in and not in operators. These operators are used to determine whether or not a given data element is part of a sequence and, in the case of the identity operator, whether or not it is of a specific type. Python Membership Operators: These …<\/p>\nPython “in” and “not in” operators<\/h2>\n
\n
1) “in”<\/strong> Operator in Python<\/h3>\n
# given list\r\ngiven_list = [\"hello\", \"this\", \"is\", \"BTechGeeks\", \"Python\"]\r\n# given element\r\nelement = \"BTechGeeks\"\r\n# checking if the given element is present in given list or not using in opertor\r\n# it returns true if is present else it returns false\r\nprint(element in given_list)\r\n<\/pre>\n
True<\/pre>\n
Here it returned true which means the given element BTechGeeks is present in given list.<\/pre>\n
# given tuple\r\ngiven_tuple = [\"hello\", \"this\", \"is\", \"BTechGeeks\", \"Python\"]\r\n# given element\r\nelement = \"BTechGeeks\"\r\n# checking if the given element is present in given tuple or not using in opertor\r\n# it returns true if is present else it returns false\r\nprint(element in given_tuple)\r\n<\/pre>\n
True<\/pre>\n
Here it returned true which means the given element BTechGeeks is present in given tuple.<\/pre>\n
# given string\r\ngiven_string = \"hello this is BTechGeeks Python\"\r\n# given element\r\nelement = \"BTechGeeks\"\r\n# checking if the given element is present in given string or not using in opertor\r\n# it returns true if is present else it returns false\r\nprint(element in given_string)\r\n<\/pre>\n
True<\/pre>\n
Here it returned true which means the given element BTechGeeks is present in given string.<\/pre>\n
2) “not in”<\/strong> Operator in Python<\/h3>\n
# given list\r\ngiven_list = [\"hello\", \"this\", \"is\", \"BTechGeeks\", \"Python\"]\r\n# given element\r\nelement = \"BTechGeeks\"\r\n# checking if the given element is present in given list or not using not in opertor\r\n# it returns true if is present else it returns false\r\nprint(element not in given_list)\r\n<\/pre>\n
False<\/pre>\n
Here it returned False which means the given element BTechGeeks is present in given list.<\/pre>\n
# given tuple\r\ngiven_tuple = [\"hello\", \"this\", \"is\", \"BTechGeeks\", \"Python\"]\r\n# given element\r\nelement = \"BTechGeeks\"\r\n# checking if the given element is present in given tuple or not using not in opertor\r\n# it returns true if is present else it returns false\r\nprint(element not in given_tuple)\r\n<\/pre>\n
False<\/pre>\n
Here it returned False which means the given element BTechGeeks is present in given tuple.<\/pre>\n
# given string\r\ngiven_string = \"hello this is BTechGeeks Python\"\r\n# given element\r\nelement = \"BTechGeeks\"\r\n# checking if the given element is present in given string or not using not in opertor\r\n# it returns true if is present else it returns false\r\nprint(element not in given_string)\r\n<\/pre>\n
False<\/pre>\n
Here it returned False which means the given element BTechGeeks is present in given string.<\/pre>\n
3)Working of Membership Operators (“in” and “not in” ) in Dictionaries:<\/h3>\n
# given dictionary\r\ndictionary = {\"hello\": 100,\r\n \"This\": 200,\r\n \"BTechGeeks\": 300}\r\n# checking using in and not in operators\r\nprint(100 in dictionary)\r\nprint(100 not in dictionary)\r\n\r\nprint(\"BTechGeeks\" in dictionary)\r\nprint(\"BTechGeeks\" not in dictionary)\r\n<\/pre>\n
False\r\nTrue\r\nTrue\r\nFalse<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n