5️⃣ Membership Operators

There are two membership operators present in python.

1) in:

It returns True if the particular value is member of object(list, tuple, dict, etc). Lets define list containing numbers.

💻 Example:

1
2
3
a = [1, 2, 3, 5]
print(4 in a)
print(3 in a)

✅ Output:

False
True

1) not in:

It returns True if the particular value is not member of object(list, tuple, dict, etc). Lets refer the same list containing numbers.

💻 Example:

1
2
3
a = [1, 2, 3, 5]
print(4 not in a)
Print(3 not in a)

✅ Output:

True
False

💻 Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Python program to illustrate 'in', 'not in' operator
x = 24
y = 20
my_list = [10, 20, 30, 40, 50]

if (x not in my_list):
    print("x is NOT present in given list")
else:
    print("x is present in given list")

if (y in my_list):
    print("y is present in given list")
else:
    print("y is NOT present in given list")

✅ Output:

x is NOT present in given list
y is present in given list

Be prepared with below questions prior to your interview !

Frequently asked Interview Questions
  1. What are membership operators in Python?

  2. How does the ‘in’ operator work in Python?

  3. Explain the ‘not in’ operator in Python.

  4. Can the ‘in’ and ‘not in’ operators be used with strings?