in Operator
in Membership Operator is used to check if specific value is present in given collection.
Syntax
The syntax of in Operator with the operands is
x in collection
where x is a value, and collection is any collection of items like a list, tuple, set, string, etc.
in operator takes two operands as shown in the above expression. Left operand is the value, and right operand is the collection.
in operator returns a boolean value. If the element or value x is present in the collection, then the above expression returns a of True, or else the expression returns False.
Example
In this example, we take a list of strings, and check if the value "mango" is present in this list, using in keyword.
Python Program
fruits = ['apple', 'banana', 'cherry', 'mango']
x = 'mango'
if x in fruits:
print('x is in the given list.')
else:
print('x is not in the given list.')
Run Code CopyOutput
x is in the given list.
Summary
In this tutorial of Python Examples, we learned about in Membership Operator, and how to use it to check if specific element is present in a given collection.