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 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.
“in” operator(brief):
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.
“not in ” operator (brief) :
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.
Python “in” and “not in” operators
- “in” Operator in Python
- “not in” Operator in Python
- Working of Membership Operators (“in” and “not in” ) in Dictionaries:
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
1) “in” Operator in Python
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.
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.
Let us use an example to gain a better grasp of how the in operator works.
1)Checking if given element is present in list using in operator.
Below is the implementation:
# given list given_list = ["hello", "this", "is", "BTechGeeks", "Python"] # given element element = "BTechGeeks" # checking if the given element is present in given list or not using in opertor # it returns true if is present else it returns false print(element in given_list)
Output:
True
Explanation:
Here it returned true which means the given element BTechGeeks is present in given list.
2)Checking if given element is present in tuple using in operator.
Below is the implementation:
# given tuple given_tuple = ["hello", "this", "is", "BTechGeeks", "Python"] # given element element = "BTechGeeks" # checking if the given element is present in given tuple or not using in opertor # it returns true if is present else it returns false print(element in given_tuple)
Output:
True
Explanation:
Here it returned true which means the given element BTechGeeks is present in given tuple.
3)Checking if given element(string) is present in string using in operator.
Below is the implementation:
# given string given_string = "hello this is BTechGeeks Python" # given element element = "BTechGeeks" # checking if the given element is present in given string or not using in opertor # it returns true if is present else it returns false print(element in given_string)
Output:
True
Explanation:
Here it returned true which means the given element BTechGeeks is present in given string.
2) “not in” Operator in Python
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.
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.
Simply replace the in operator with the not in one in the previous example.
Let us use an example to gain a better grasp of how the not in operator works.
1)Checking if given element is present in list using not in operator.
Below is the implementation:
# given list given_list = ["hello", "this", "is", "BTechGeeks", "Python"] # given element element = "BTechGeeks" # checking if the given element is present in given list or not using not in opertor # it returns true if is present else it returns false print(element not in given_list)
Output:
False
Explanation:
Here it returned False which means the given element BTechGeeks is present in given list.
2)Checking if given element is present in tuple using not in operator.
Below is the implementation:
# given tuple given_tuple = ["hello", "this", "is", "BTechGeeks", "Python"] # given element element = "BTechGeeks" # checking if the given element is present in given tuple or not using not in opertor # it returns true if is present else it returns false print(element not in given_tuple)
Output:
False
Explanation:
Here it returned False which means the given element BTechGeeks is present in given tuple.
3)Checking if given element(string) is present in string using in operator.
Below is the implementation:
# given string given_string = "hello this is BTechGeeks Python" # given element element = "BTechGeeks" # checking if the given element is present in given string or not using not in opertor # it returns true if is present else it returns false print(element not in given_string)
Output:
False
Explanation:
Here it returned False which means the given element BTechGeeks is present in given string.
3)Working of Membership Operators (“in” and “not in” ) in Dictionaries:
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.
So, do the above operators work with dictionaries? If they do, how do they respond accordingly?
Let us provide an example to help us understand.
Below is the implementation:
# given dictionary dictionary = {"hello": 100, "This": 200, "BTechGeeks": 300} # checking using in and not in operators print(100 in dictionary) print(100 not in dictionary) print("BTechGeeks" in dictionary) print("BTechGeeks" not in dictionary)
Output:
False True True False
First, we created a dictionary dictionary with a certain set of random keys and corresponding random values.
As shown in the result above, “100” in dictionary evaluates to False. In contrast, “BTechGeeks” in dict1 yields True.
As a result, it is evident that the in operator searches for the element among the dictionary keys rather than the values.
As previously stated, the not in operator here also evaluates in the same manner.
Related Programs:
- python search strings in a file and get line numbers of lines containing the string
- the difference between and is in python
- program to clear the rightmost set bit of a number in cpp and python
- program to determine all pythagorean triplets in the range in cpp and python
- python program to generate a dictionary that contains numbers between 1 and n in the form x xx
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to read height in centimeters and then convert the height to feet and inches