Membership operators

Membership operators are used to find the existence of an element in sequence or dictionary.

 

Operator

Meaning

Example

in

It returns True if an element  is found in iteration or dictionary

5 in list1

not in

It returns True if an element  is not found in iteration or dictionary

5 not in list1

 

In the case of a dictionary  'in' operator checks the existence of the key, not the value.

Example : Membership operators in Python

list = ['P', 'y', 't', 'h', 'o', 'n']

vowels={'a':1,'e':2,'i':3,'o':4,'u':5}

'a' in vowels

# Output: True

'b' in vowels

# Output: True

'b' not in vowels

# Output: True

 

print('P' in list)

# Output: True

print('p' in list)

# Output: False

​​​​​​​Output

True

False

True

True

False