Membership Operators in Python with Example

Membership operators in Python are special type of binary operators that test for membership in a sequence, such as a string, list, tuple, set, or dictionary.

The membership operators are useful to examine whether a value or variable is present in the sequence (string, list, tuple, set, or dictionary).

In the case of a dictionary, we can test only for the presence of a key but not its value.

Python language supports two types of membership operators that they are as follows:

  • in
  • not in

Let’s have a look at both Python membership operators one by one with the help of examples.

Membership In operator


This membership operator evaluates to true if the specified value is present in the specified sequence and otherwise it evaluates to false. The operator symbol for this membership operator is ‘in’.

Let’s take some example programs based on the use of membership in operator.

Example 1: Use of membership in operator with string and list

my_string = 'Every person loves his country in the world.'
print('E' in my_string)
print('country' in my_string)
print('like' in my_string)
print('lo' in my_string)

my_list = [2, 4, 6, 8, 10, 12]
print(6 in my_list)
print(5 in my_list)
Output:
      True
      True
      False
      True

      True
      False

Explanation:

a) In this example, there is ‘E’ in my_string, interpreter returned true value. Similarly, for ‘country’ in my_string, the interpreter returned true.

b) In the third print statement, the ‘like’ is not present in the sequence my_string. Therefore, the interpreter returned false value.

c) In the fourth print statement, the ‘lo’ is present in the my_string, the output is true.

d) In the case of my_list, the value 5 is not present in the sequence list. Therefore, the interpreter returned false value.


Example 2: Use of membership in operator with dictionary

my_dict = {
    1: 'Orange',
    2: 'Banana',
    3: 'Apple'
}
print(1 in my_dict)
print(3 in my_dict)
print('Banana' in my_dict)
Output:
      True
      True
      False

As you can see in the output, we can only test the presence of keys in the dictionary, not their values.

Membership Not in operator


This membership operator evaluates to true if the specified value is not present in the specified sequence and otherwise it evaluates to false value. The operator symbol for this membership operator is ‘not in’.

Let’s take some example programs based on the use of membership not in operator.

Example 1:

my_string = 'I love Python programming'
print('love' not in my_string)
print('like' not in my_string)
print('pro' not in my_string)

my_list = [10, 20, 30, 40, 50]
print(15 not in my_list)
print(20 not in my_list)
Output:
      False
      True
      False
      True
      False

Explanation:

a) In the first print statement, ‘love’ is present in the my_string. Therefore, the interpreter returned false value.

b) In the second print statement, ‘like’ is not present in the my_string. Therefore, the interpreter returned true value.

c) In the third print statement, ‘pro’ is present in the my_string. Hence, the interpreter returned false value.

d) Similarly, for my_list.


Example 2:

num1  = 10
num2 = 30
my_list = [10, 20, 40, 50, 60 ]
if(num1 in my_list):
    print(num1, 'is present in the given list.')
else:
    print(num1, 'is not present in the given list.')

if(num2 not in my_list):
    print(num2, 'is not present in the given list.')
else:
    print(num2, 'is present in the given list.')
Output:
      10 is present in the given list.
      30 is not present in the given list.

In this tutorial, we have covered about another special type of membership operators in Python with examples. Hope that you will have understood the basic points of ‘in’ and ‘not in’ membership operators and practiced all example programs.
Thanks for reading!!!
Next ⇒ Ternary Operator in Python⇐ Prev Next ⇒