# Python “in” and “not in” Membership Operators

Filed Under: Python

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

The Python in operator is used to check for the presence of a specific element in a sequence. This operator can be used with loops and conditions and even just to ensure that a specific value is present when taking user input.

This is a boolean operator and returns True if the element is present and False if the element is not present within the given sequence.

Not let us take an example to get a better understanding of the `in` operator working.

```x in y
```

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

Let’s implement a simple Python code to demonstrate the use of the in operator and how the outputs would look like.

```vowels = ['A', 'E', 'I', 'O', 'U']

ch = input('Please Enter a Capital Letter:\n')

if ch in vowels:
print('You entered a vowel character')
else:
print('You entered a consonants character')
```

Recommended Readings: Python input(), Python List

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

```>>> name='JournalDev'
>>> 'D' in name
True
>>> 'x' in name
False
>>> primes=(2,3,5,7,11)
>>> 3 in primes
True
>>> 6 in primes
False
```

## Can we use Python “in” Operator with a Dictionary?

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

```dict1 = {"name": "Pankaj", "id": 1}

print("name" in dict1)  # True
print("Pankaj" in dict1)  # False
```

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

## Python “not in” Operator

The not in operator is the opposite of the in operator. While it checks the presence of a particular element within a sequence, it returns the opposite value.

It behaves in the same way it would behave in terms of the English language. So when want to ensure that 6 is not in between 1-5, we know that to be true. Because 6 does not fall within the sequence.

Let’s clarify that further with a few examples. We will take the previous example, and replace the `in` operator with the `not in` operator for this demonstration.

```>>> primes=(2,3,5,7,11)
>>> 6 not in primes
True
>>> 3 not in primes
False
```

## Conclusion

The in and the not in operators are espcially useful to quickly check for specific elements without using the equality checks. They’re more readable to other coders as well when you’re working with a team.

I hope you now understand both the operators well enough! We’d love to hear your thoughts and the comments are open for you to let us know what you think. Also, feel free to drop in any questions that you may have about these two operators.