Python "in" and "not in" Membership Operators With Examples

Python has two membership operators – “in” and “not in”. They are used to check if an element is present in a sequence or not.

Python in Operator

This operator returns True if the specified element is present in the sequence. The syntax of “in” operator is:

Here “x” is the element and “y” is the sequence where membership is being checked.

Here is a simple program to show the usage of Python in operator.

Python in Operator Example

Recommended Readings: Python input(), Python List

We can use the “in” operator with Strings and Tuples too because they are sequences.

Can we use Python “in” Operator with Dictionary?

Let’s see what happens when we use “in” operator with a dictionary.

It looks like the Python “in” operator looks for the element in the dictionary keys.

Python “not in” Operator

It’s opposite of the “in” operator. We can use it with sequence and iterables.

By admin

Leave a Reply

%d bloggers like this: