Identity & membership operators

Table of contents:

Python identity and membership operators

To go through this page, you should have some idea about python lists and tuples.

Python identity operators is and is not

Python has two identity operators to check whether two python objects have the same identity(id).

is variable is variable
is not variable is not variable

Table of contents:

The is operator

When two variables have the same identities, the is returns a True, when they have different identities, is returns a False, even if their values are equal.

We use multiple assignment to create two variables and assign them the same value.

Because both the variables hold the integer literal 257, the expression p == q results in a True as per the functioning of comparison operators.

In the expression p is q, the identities of the two variables are compared. Let us check their identities:

We see that the identities of p and q are the same. So p is q returned True.

The above results conclude that both the variables contain the same literal 257, and they point to the same integer object.

However, there is an exception. If we define the variables seperately, as done in the following, the output differs:

Here, two different objects with different identities are created, so p is q returned False. This exception occurs only in interactive mode. In script mode, with the above code p is q will return True.

The following statements when executed in interactive mode show that the is operator behaves unexpectedly with different literals.

Codep is qObjects createdIdentities
p = -6 False two different
q = -6
p = -5 True one same
q = -5
p = 256 True one same
q = 256
p = 257 False two different
q = 257

In python, small integers in the range of -5 to 256 are treated differently than the other integers.

However, to avoid any confusion, it is recommended not to use the is operator which compares identities, rather use the == operator which compares values.

The above recommendation specially applies for immutable objects like 'int', 'float', 'str' etc.

The is not operator

The is not operator is exactly opposite to the is operator.

If x is y returns False, x is not y will return True.

x is y returns False, so x is not y returns True.

n is m returns True, so n is not m returns a False.

Since n is m and n is not m are valid python expressions, they can be assigned to variables.

Python membership operators in and not in

Before going into the details of python identity and membership operators, please go through the page python lists and python tuples to understand the conception of python collection objects.

Python has two membership operators to check whether a certain object is present in a collection of objects.

in value in collection
not in value not in collection

Python membership operator in

The operator in checks whether an item is a member of a collection object like list, tuple etc. If the item is member, a True is returned, else a False is returned.

We create a variable mylist and assign a list to it.

In the above, is the item 1 is a member of the list [1, 3, 5, 7, 9] ? Yes, true. The first element of the list is 1. So, a True is returned.

Is the item 10 is a member of the list ? No, false. 10 does not belong to this list. So, a False is returned.

We can even do this without any variable as in the following:


Note that numeric literal 7 is present in the list as the 3rd element, so True is the output. But no string literal "7" is present in the given list, so a False is returned in response to the fourth line. We have already seen that "7" and 7 are not the same in python.

Python tuples are also immutable collection of objects. With the in and not in we can check if a certain data is present in a tuple.

Both "Assam" and False values are members of the given tuple. So both expressions return True.

Neither 2718 nor 85 are present in the given tuple. So both expressions return False. Note again that 85 and "85" are not the same in python.

In the above,
10 in mylist, 85 in mytuple,
7 in [1, 3, 5, 7, 9]
are all valid boolean expressions. They evaluate to either True or False values.

Python strings are sequence of characters, so with membership operators we can check if a particular character is present in a given string or not.

We can even check if a particular word is part of the string or not:

Python membership operator not in

The operator not in is the inverse of in operator.
With not in operator, if the operand is a member of the collection, a False is returned, else a True is returned.

Because 1 in mylist results a True, ∴ 1 not in mylist will result a False.

Because 10 in mylist results a False, ∴ 10 not in mylist will result a True.

The character "Z" is not a part of the string "INDIA", so "Z" not in "INDIA" returns a True.

The character "D" is a part of the string "INDIA", so "D" not in "INDIA" returns a False.

Two lists and a number are given. Write a boolean expression which is evaluated to True if the given number is present in both of the lists, else the expression shoud return a False.

We write a script to implement the above.

The above logic is written correctly. In the fourth line, if you write the logic as number in evens and primes, you will get an unexpected result.

Now change the value of number to anything except 2, you will get the output False.

--- x ---

Want to leave a message for me?