Python defines the following 2 special operators
- Identity Operators
- Membership operators
Identity Operators
- We can use identity operators for address comparison.
- There are 2 identity operators are available
- is
- is not
Remark
- r1 is r2 ⇒ returns True if both r1 and r2 are pointing to the same object.
- r1 is not r2 ⇒ returns True if both r1 and r2 are not pointing to the same object.
Example
Python
num1 = 10
num2 = 10
print(num1 is num2)
bool1 = True
bool2 = True
print(bool1 is bool2)
Output
PowerShell
True
True
Example
Python
str1 = "ajay"
str2 = "ajay"
print(id(str1))
print(id(str2))
print(str1 is str2)
Output
PowerShell
139689637218672
139689637218672
True
Example
Python
list1 = ["one", "two", "three"]
list2 = ["one", "two", "three"]
print(id(list1))
print(id(list2))
print(list1 is list2)
print(list1 is not list2)
print(list1 == list2)
Output
PowerShell
140194451674432
140194451690752
False
True
True
Remark
- We can use is operator for address comparison where as == operator for content comparison.
Membership operators
- We can use Membership operators to check whether the given object present in the given collection (String, List, Set, Tuple or Dict).
- There are 2 identity operators are available
- In ⇒ Returns True if the given object present in the specified Collection.
- not in ⇒ Returns True if the given object not present in the specified Collection.
Example
Python
str1 = "hello learning Python is very easy!!!"
print('l' in str1)
print('d' in str1)
print('d' not in str1)
print('Python' in str1)
Output
PowerShell
True
False
True
True
Example
Python
list1 = ["kuldeep", "ajay", "amit"]
print("ajay" in list1)
print("manish" in list1)
print("manish" not in list1)
Output
PowerShell
True
False
True
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!