In this tutorial, let us look at the difference between != and is not operator in Python.
!= is defined in Python as the not equal to operator. If the operands on either side are not equal, it returns True; otherwise, it returns False.
The is not operator checks whether the id() of two objects is the same or not. If they are the same, it returns False; otherwise, it returns True. And the is not operator returns True if the operands on either side are not equal, and False if they are.
Difference between != and is not operator in Python
- Using is not Operator on Numbers
- Using is not Operator on Strings
- Using is not Operator on Lists
- Using != Operators on Numbers
- Using != Operators on Strings
- Using != Operators on Lists
- Comparing != and is not operators
Method #1: Using is not Operator on Numbers
Approach:
- Give the first number as static input and store it in a variable.
- Give the second number as static input and store it in another variable.
- Check if both the given numbers are equal or not using the is not operator.
- Print the id of both the given numbers using the id() function.
- The Exit of the Program.
Below is the implementation:
# Give the first number as static input and store it in a variable. gvn_num1 = 5 # Give the second number as static input and store it in another variable. gvn_num2 = 5 # Check if both the given numbers are equal or not using the is not operator print(gvn_num1 is not gvn_num2) # Print the id of both the given numbers using the id() function print(id(gvn_num1), id(gvn_num2))
Output:
False 11256192 11256192
Explanation:
Here it returns the output as False since both variables gvn_num1 and gvn_num2 referred to the same data 5
Method #2: Using is not Operator on Strings
Approach:
- Give the first string as static input and store it in a variable.
- Give the second string as static input and store it in another variable.
- Check if both the given strings are equal using the is not operator.
- Print the id of both the given strings using the id() function.
- The Exit of the Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_str1 = "pythonprogarms" # Give the second string as static input and store it in another variable. gvn_str2 = "pythonprogarms" # Check if both the given strings are equal using the is not operator print(gvn_str1 is not gvn_str2) # Print the id of both the given strings using the id() function print(id(gvn_str1), id(gvn_str2))
Output:
False 140627397413232 140627397413232
Explanation:
Here it returns the output as False since both the string variables gvn_str1 and gvn_str2 referred to the same data "pythonprogarms"
Method #3: Using is not Operator on Lists
# Give the first list as static input and store it in a variable. gvn_lst1 = [10, 15, 20] # Give the second list as static input and store it in another variable. gvn_lst2 = [10, 15, 20] # Check if both the given lists are equal using the is not operator print(gvn_lst1 is not gvn_lst2) # Print the id of both the given lists using the id() function print(id(gvn_lst1), id(gvn_lst2))
Output:
True 140627403656880 140627397425344
Explanation:
Here it returns the output as True since both the variables gvn_lst1 and gvn_lst2 have distinct memory addresses even if both variables contain the same data.
Method #4: Using != Operators on Numbers
Approach:
- Give the first number as static input and store it in a variable.
- Give the second number as static input and store it in another variable.
- Check if both the given numbers are equal or not using the != operator.
- Print the id of both the given numbers using the id() function.
- The Exit of the Program.
Below is the implementation:
# Give the first number as static input and store it in a variable. gvn_num1 = 5 # Give the second number as static input and store it in another variable. gvn_num2 = 5 # Check if both the given numbers are equal or not using the != operator print(gvn_num1 != gvn_num2) # Print the id of both the given numbers using the id() function print(id(gvn_num1), id(gvn_num2))
Output:
False 11256192 11256192
Method #5: Using != Operators on Strings
Approach:
- Give the first string as static input and store it in a variable.
- Give the second string as static input and store it in another variable.
- Check if both the given strings are equal or not using the != operator.
- Print the id of both the given strings using the id() function.
- The Exit of the Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_str1 = "pythonprogarms" # Give the second string as static input and store it in another variable. gvn_str2 = "pythonprogarms" # Check if both the given strings are equal or not using the != operator print(gvn_str1 != gvn_str2) # Print the id of both the given strings using the id() function print(id(gvn_str1), id(gvn_str2))
Output:
False 140627397413232 140627397413232
Method #6: Using != Operators on Lists
# Give the first list as static input and store it in a variable. gvn_lst1 = [10, 15, 20] # Give the second list as static input and store it in another variable. gvn_lst2 = [10, 15, 20] # Check if both the given lists are equal or not using the != operator print(gvn_lst1 != gvn_lst2) # Print the id of both the given lists using the id() function print(id(gvn_lst1), id(gvn_lst2))
Output:
False 140627397115488 140627397114768
Method #7: Comparing != and is not operators
gvn_lst1 = [] gvn_lst2 = [] gvn_lst3 = gvn_lst1 # Applying != operator for comparing gvn_lst1 and gvn_lst2 if (gvn_lst1 != gvn_lst2): print("True") else: print("False") # Applying is not operator for comparing gvn_lst1 and gvn_lst2 if (gvn_lst1 is not gvn_lst2): print("True") else: print("False") # Applying is not operator for comparing gvn_lst1 and gvn_lst3 if (gvn_lst1 is not gvn_lst3): print("True") else: print("False") # Concatenating given third and secondlists and saving it as a given third list gvn_lst3 = gvn_lst3 + gvn_lst2 # Applying is not operator for comparing gvn_lst1 and gvn_lst3 if (gvn_lst1 is not gvn_lst3): print("True") else: print("False")
Output:
False True False True
Explanation:
- Because both gvn_lst1 and gvn_lst2 are empty lists, the output of the first condition is “False”.
- Second, if the condition is “True,” it means that two empty lists are located in different memory locations. As a result, gvn_lst1 and gvn_lst2 correspond to distinct items. We can test it using Python’s id() function, which returns an object’s “identity.”
- If the condition is “False,” the output of the third is “False,” because both gvn_lst1 and gvn_lst3 point to the same object.
- Since the concatenation of two lists always produces a new list, the output of the fourth if the condition is “True.”