Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

Python Membership and Identity operators

Membership and Identity Operators

Hello, readers! In this article, we will be focusing on Python Membership and Identity operators in detail.

So, let us begin!! 🙂


Python Membership and Identity Operators – Quick Overview!

Python offers us various operators to perform manipulation and operations on the data values and variables at a broader scale. In the context of this article, we would be primarily focusing on two important types of operators in Python:

  1. Membership Operators
  2. Identity Operators

So now, let us go ahead and understand the functioning about each one of them in the upcoming section.


Python Membership Operators – [‘in’, ‘not in’]

Python Membership Operators helps us evaluate and validate the membership of the data values in a particular sequence present in data structures such as list, tuple, etc. By this, we mean to say, it checks for the presence of the given sequence of data into another sequence or structure and validates the same.

1. Python ‘in’ Membership Operator

The in operator is a boolean operator that checks for the presence of a particular data sequence in a data structure and returns true if found. Else, it returns false.

Example:

In this example, we search for the data sequence values (10,20) in the list (list1). Being found, it prints the resultant statement.

lst1=[10,20,30,40,50]
lst2=[10,6,20,7]
for x in lst1:
	if x in lst2:
		print("Data overlaps for value:", x)	

Output:

Data overlaps for value: 10
Data overlaps for value: 20

2. Python ‘not in’ Membership Operator

The not in operator results to TRUE, if it does not encounter a given data value in a sequence such as list, string, etc.

Example:

In this example, the data value ’32’ is not present in the list, therefore it returns false and prints the statement after if condition.

lst=[10,20,30,40,50]
data = 32
if data not in lst:
   print("Data not found")
else:
   print("Data is present")

Output:

Data not found

Python Identity Operators – [‘is’, ‘is not’]

The Identity operators in Python helps us have a check on the equality of the values in terms of what memory location they are pointing to, have the same data type as expected, etc.

1. Python ‘is’ Identity Operator

With ‘is’ operator, we can easily check for the validity of the values on the either side whether they point to the same memory point or have same data type or the desired data type, etc.

Example:

In the below example, we have used is operator to check if the data value is of type float. It returns TRUE if the condition satisfies, else would return false.

data = 40.03
if type(data) is float:
	print("TRUE")
else:
	print("FALSE")

Output:

TRUE

2. Python ‘is not’ Identity operator

With ‘is not’ operator, we check the validity against the equality or the above conditions, and if they do not satisfy, it returns TRUE. If the conditions satisfy, it returns FALSE.

data = 40.03
if type(data) is not int:
	print("Not same")
else:
	print("same")

Output:

Not same

Conclusion

Feel free to comment below, in case you come across any questions. For more such posts related to Python programming, stay tuned with us. Till then, Happy Learning!! 🙂