Python Program for Membership and Identity Operators

Python provides a number of operators for performing manipulation and operations on data values and variables on a larger scale.

Let us see the two important types of Python operators:

  • Membership Operators
  • Identity Operators

Membership Operators (‘in’, ‘not in’)

Membership operators are used to validate a value’s membership. It checks for membership in a sequence, such as strings, lists, or tuples.

This means that it checks for the presence of the given data sequence in another sequence or structure and validates it.

‘in’ – Membership operator:

The ‘in’ operator is used to determine whether or not a value exists in a sequence. If it finds a variable in the given sequence, it returns true; otherwise, it returns false.

For Example:

Approach:

  • Give the first list as static input and store it in a variable.
  • Give the second list as static input and store it in another variable.
  • Iterate in the given first list using the for loop and ‘in’ Operator.
  • Inside the for loop, Check if the element present in the first list is also present in the second list using the if conditional statement and ‘in’ Operator.
  • If it is true, then print the respective element of the first list.
  • The Exit of the Program.

Below is the implementation:

# Give the first list as static input and store it in a variable.
gvn_fstlst = [4, 7, 1, 15, 2]
# Give the second list as static input and store it in another variable.
gvn_scndlst = [7, 6, 2, 5]
# Iterate in the given first list using the for loop and 'in' Operator
for numb in gvn_fstlst:
    # Check if the element present in the first list is also present in the
    # second list using the if conditional statement and 'in' Operator.
    if numb in gvn_scndlst:
        # If it is true, then print the respective element of the first list.
        print(numb, "is common in both the lists")

Output:

7 is common in both the lists
2 is common in both the lists

‘not in’ – Membership operator:

If the ‘not in’ operator does not encounter a given data value in a sequence such as a list, string, etc., it returns True.

For Example:

Approach:

  • Give the list as static input and store it in a variable.
  • Give the number as static input and store it in another variable.
  • Check if the given number is not present in the given list using the if conditional statement and ‘not in’ Operator.
  • If it is true, then print the respective element of the first list.
  • If it is true, then print “The given number is NOT present in the given list”.
  • Else print “The given number is present in the given list”.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [4, 7, 1, 15, 2]
# Give the number as static input and store it in another variable.
gvn_numb = 15
# Check if the given number is not present in the given list using the
# if conditional statement and 'not in' Operator.
if gvn_numb not in gvn_lst:
    # If it is true, then print "The given number is NOT present in the given list"
    print("The given number", gvn_numb, "is NOT present in the given list")
else:
    # Else print "The given number is present in given the list"
    print("The given number", gvn_numb, "is present in the given list")

Output:

The given number 15 is present in the given list

Identity Operators (‘is’, ‘is not’)

The Identity operators in Python allow us to check the equivalence of values in terms of what memory location they are pointing to, have the same data type as expected, and so on.

‘is’ – Identity operator:

If the variables on either side of the operator point to the same object, the result is true; otherwise, the result is false.

For Example:

Approach:

  • Give the number as static input and store it in a variable.
  • Check if the datatype of the given number is a float value using the if conditional statement and ‘is’ identity Operator.
  • If it is true, then print “The given number is a floating-point number”.
  • Else print “The given number is NOT a floating-point number”.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in another variable.
gvn_numb = 15.25
# Check if the type of the given number is a float value using the
# if conditional statement and 'is' identity Operator.
if type(gvn_numb) is float:
    # If it is true, then print "The given number is a floating-point number"
    print("The given number", gvn_numb, "is a floating-point number")
else:
    # Else print "The given number is NOT a floating-point number"
    print("The given number", gvn_numb, "is NOT a floating-point number")

Output:

The given number 15.25 is a floating-point number

‘is not’ – Identity operator:

If the variables on either side of the operator point to the same object, the result is false; otherwise, the result is true.

For Example:

Approach:

  • Give the number as static input and store it in a variable.
  • Check if the datatype of the given number is not a float value using the if conditional statement and ‘is not’ identity Operator.
  • If it is true, then print “The given number is NOT a floating-point number”
  • Else print “The given number is a floating-point number”
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in another variable.
gvn_numb = 10
# Check if the type of the given number is not a float value using the
# if conditional statement and 'is not' identity Operator.
if type(gvn_numb) is not float:
    # If it is true, then print "The given number is NOT a floating-point number"
    print("The given number", gvn_numb, "is NOT a floating-point number")
else:
    # Else print "The given number is a floating-point number"
    print("The given number", gvn_numb, "is a floating-point number")

Output:

The given number 10 is NOT a floating-point number