Python Membership and Identity Operators

Python Membership And Identity Operators

Python is a high-level programming language widely used in various areas of software development. One of the fundamental concepts in Python is the use of operators, which are used to perform specific operations on values or variables. Python membership and identity operators are two such operators that can be used to compare values, variables and objects.

Understanding Python membership and identity operators is important for any programmer looking to develop efficient and optimized code. This article will detail the membership and identity operators, their use cases and examples.

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 will be primarily focusing on two essential types of operators in Python:

  1. Membership Operators
  2. Identity Operators

For the membership operator, we will learn about the ‘in’ and ‘not in’ operators and demonstrate it with an example, and for the identity operator we will see ‘is’ and ‘is not’ and demonstrate it with an example as well. So now, let us go ahead and understand the function of each of them.

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

Membership Operators in Python help us evaluate and test for membership of a value in a particular sequence present in data structures such as lists, tuples, etc. By this, we mean to say, it checks for the presence of the given sequence of data in another sequence or structure and validates the same.

Python ‘in’ Membership Operator

The ‘in’ operator is used to check for the presence of a particular element in a data structure that the element is present or not. It returns a boolean True if present, 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

Python ‘not in’ Membership Operator

The not in‘ operator results to True, if it does not encounter a given element in a sequence such as a list, string, etc.

Example:

In this example, the data value ’32’ is not present in the list, therefore it returns True 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 are used to check the equality of the values in terms of what memory location they are pointing to.

Python ‘is’ Identity Operator

With the ‘is’ operator, we can easily check if the variables on either side of the operator point to the same object in memory. It does not check for the same data type or desired data type. If they point to the same object, the ‘is’ operator returns True, otherwise, it returns False.

Example:

In the below example, we have used the ‘is’ operator to check if the data value is of type float. It returns True if the condition is satisfied, else would return False.

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

Output:

TRUE

Python ‘is not’ Identity Operator

With the ‘is not’ operator, we check the validity against the equality of a condition, 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

In this tutorial, we have learned about the Membership operator and Identity operator. Membership operators are used to check whether a value is present in a given sequence and identity operators are used to check whether two objects are the same object in memory. We hope you enjoyed reading this tutorial.

Reference

https://docs.python.org/3/reference/expressions.html#comparisons