Python Tutorial

Python Special Operators (Identity & Membership Operators): Explained With Examples

What Are Special Operators in Python?

Special operators in Python are a set of operators that go beyond the standard arithmetic and logical operators. These operators provide additional functionality to perform specific tasks or comparisons in a more specialized manner. 

By understanding and utilizing Python’s special operators effectively, you can enhance your code's readability, efficiency, and accuracy. Here, we will specifically focus on identity and membership operators in Python and explore their usage in more detail.

Identity Operators in Python

The identity operators in Python are used to compare the identity of two objects, which means they check whether two objects refer to the same memory location. The identity operators available in Python are "is" and "is not".

"is" operator:

  • The "is" operator returns True if two objects have the same identity, indicating that they are the same object.

  • It compares the memory addresses of the objects rather than their values.

  • If the objects have different memory addresses, even if their values are the same, the "is" operator will return False.

The syntax for the "is" operator is: 

object1 is object2.

Example:

a = [1, 2, 3]
b = a
print(a is b)  # True - a and b refer to the same list object
print(a is not b)  # False - a and b have the same identity, so they are not not the same object

Output:

True

False

"is not" operator:

  • The "is not" operator returns True if two objects have different identities, indicating that they are not the same object.

  • It is the negation of the "is" operator.

The syntax for using the "is not" operator is: 

object1 is not object2.

Example :

x = 5
y = 10
print(x is y)  # False - x and y have different identities
print(x is not y)  # True - x and y are not the same object

Output:

False

True

*Note: It's important to distinguish between the identity operators ("is" and "is not") and the equality operators ("==" and "!="). While the identity operators compare the memory addresses, the equality operators compare the values of the objects. Therefore, two objects can have the same value but different identities, resulting in the identity operators returning False while the equality operators return True.

Membership Operators in Python

Membership operators are used to check the membership of a value in a sequence or collection, such as strings, lists, tuples, or sets. The membership operators available in Python are "in" and "not in".

"in" operator:

  • The "in" operator returns True if a value is found in the given sequence or collection.

  • It checks whether the value is present within the sequence and returns True if it is, and False otherwise.

Example:

text = "Hello, world!"
print("Hello" in text)  # True - "Hello" is present in the string
print("hi" in text)  # False - "hi" is not present in the string

Output:

True

False

"not in" operator:

  • The "not in" operator returns True if a value is not found in the given sequence or collection.

  • It checks whether the value is absent within the sequence and returns True if it is, and False otherwise.

Example:

fruits = ["apple", "banana", "cherry"]
print("grape" not in fruits)  # True - "grape" is not present in the list
print("banana" not in fruits)  # False - "banana" is present in the list

Output:

True

False

The membership operators are particularly useful when you need to quickly check if a value exists within a sequence or collection. They provide a concise and efficient way to perform membership tests in Python.

*Note: The membership operators are based on the presence or absence of values, not the specific positions or indexes within the sequence or collection.

Difference Between Identity vs Equality Operators in Python

Here's a comparison between Python identity and equality operators:

 

 

 

Identity Operators

Equality Operators

Operator

is

== and !=

Purpose

Compare object identity

Compare object equality based on values

Comparison Basis

Memory addresses of objects

Values of objects

Syntax

object1 is object2

object1 == object2 or object1 != object2

Returns

True if objects have the same identity, False otherwise

True if objects have equal values, False otherwise

Usage

Mostly used for comparing objects, especially mutable objects

Used for comparing values of objects

Example

a is b returns True if a and b refer to the same object

x == y returns True if x and y have the same value

Negation

is not

!=

Example

a is not b returns True if a and b do not refer to the same object

x != y returns True if x and y have different values

Key points:

  • Identity operators (is, is not) compare the memory addresses of objects, while equality operators (==, !=) compare the values of objects.

  • Identity operators are primarily used for checking object identity and are more commonly used with mutable objects.

  • Equality operators are used to compare values and are applicable to various object types.

  • The negation of the identity operator is is not, while the negation of the equality operator is !=.

  • The choice between identity and equality operators depends on whether you want to compare object identity or values.

Remember that the choice of the operator depends on the specific requirements of your code, as well as the nature of the objects you are comparing.

Did you find this article helpful?