In this article, we are discussing the Special Operators in Python and their types.
Special operators in Python provide additional functionality to perform specific tasks or comparisons in a more specialized manner. These Operators in Python are a set of operators that go beyond the standard arithmetic,logical ,Relational and bitwise Operators.
Python language offers two special types of operators like the Membership operator and the Identity operator.
Membership and Identity Operators in Python
Here in this article Lets Learn identity and membership operators in Python and explore their usage in more detail.
Membership operator
Membership operators are special types of Operators in Python, they are useful in scenarios where you need to check whether a value exists in a sequence or not, such as a list, tuple, or string. These operators also test a sequence, such as a string, a list, or a tuple, for membership. They check whether a sequence appears in an object or not. These operators can be use in other iterable objects in python.
The membership operators available in Python are
1. in Operator:
is the one of the membership operator in python used to determine if a value is present in a sequence or not. If the specified variable or literal is found in sequence , the in operator will return true, otherwise, it will return false. By using the in operator, we can easily check whether a value is present in a sequence or not (list, tuples, set etc.) without having to loop through the entire sequence.
e.g. in Membership Operator
x=’Well come to Python’
b=[10,20,45,76,32,12]
# check if ‘P’ is present in string x
print(‘P’ in x) # prints True
# check if number 20 is present in List b
print(20 in b) # prints True
2. not in Operator:
The not in operator in python determines if the value is present in sequence or not. If the specified variable value or literal is not found in sequence then the not in operator will return true, otherwise, it will return false.
By using the not in operator, we can easily check whether a value is present in a sequence or not (list, tuples, set etc.) without having to loop through the entire sequence.
e.g. not in membership operator
x=’Well come to Python’
b=[10,20,45,76,32,12]
# check if ‘P’ is present in string x
print(‘CPP’ not in x) # prints True
# check if number 20 is present in List b
print(20 not in b) # prints False
in not in Membership Operators in Python
#Python script to demonstrate membership operator in Python
# using in, not in operator
fruits = [" Orange”,” apple", "banana",” Mango”, "cherry"]
if "Mango" in fruits:
print("Yes, Mango is a fruit!")
# using "not in" operator
if "Guva" not in fruits:
print("Yes, Guva is not in the list of fruits")
Output:
Yes, Mango is a fruit!
Yes, Guva is not in the list of fruits
Explanation:
In the above Python script, the in membership operator is used to check if “Mango" is a member of the fruits list. The Mango is present in the fruits list so the statement if "Mango" in fruits: evaluates true and we got the result Yes, Mango is a fruit!.
Similarly
“Guva" is not a member of the fruits list. The “Guva” not is present in the fruits list so the statement if "Guva" not in fruits: evaluates true and we got the result Yes, Guva is not in the list of fruits
.
Application of Membership Operator
The membership operator is widely used in various fields, including mathematics, computer science, and artificial intelligence. Here are some examples of its applications:
Set theory: The membership operator is used to define sets and determine the membership of elements in the set. It is a fundamental concept in set theory that is used to perform set operations such as union, intersection, and complement.
Artificial intelligence: The membership operator is used in fuzzy logic to represent the degree of membership of an element in a set. Fuzzy logic is used to model systems that have uncertain or imprecise information.
Database management: The membership operator is used in database management to perform operations on sets of data. It is used to filter data and retrieve records that meet a specific condition.
Natural language processing: The membership operator is used in natural language processing to determine the similarity between words and phrases. It is used to identify synonyms and related words in a corpus of text.
Programming languages: The membership operator is used in programming languages to test whether a value is a member of a list or a tuple. For example, in Python, the ‘in’ keyword is used to test the membership of an element in a list or a tuple.
Identity Operator in Python
Python provides two operators, is and is not, that determine whether the given operands have the same identity—that is, refer to the same object. This is not the same thing as equality, which means the two operands refer to objects that contain the same data but are not necessarily the same object.
Here is an example of two object that are equal but not identical:
p = 2001
q = 2000 + 1
print(p, q)
print(p==q)
print(p is q)
output
2001 2001
True
False
Here, p and q both refer to objects whose value is 2001. They are equal. But they do not reference the same object, as you can verify:
>>> id(p)
60307920
>>> id(q)
60307936
p and q do not have the same identity, and p is q returns False.
You saw previously that when you make an assignment like p = q, Python merely creates a second reference to the same object, and that you could confirm that fact with the id() function. You can also confirm it using the is operator:
>>> p = 'I am a string'
>>> q = p
>>> id(p)
55990992
>>> id(q)
55990992
>>> p is q
True
>>> p == q
True
In this case, since p and q reference the same object, it stands to reason that p and q would be equal as well.
Unsurprisingly, the opposite of is is, is not:
p = 10
q = 20
p is not q
True