Identity Operators and Membership Operator in Python
Identity Operators and Membership Operator in Python
Identity
Operators
Identity operators are used to compare the objects, not if they
are equal, but if they are actually the same object, with the same memory
location:
ये चेक करेगा की दोनों object या variables
या value बराबर हैं या नहीं यदि हैं तो true
return करेगा वरना False return करेगा।
Operator |
Description |
Example |
is |
Returns true if both variables are the same object |
x is y |
is not |
Returns true if both variables are not the same object |
x is not y |
Example:-
X = 10
Y = 10
Print(x is y, x==y)
Output: True, True
X = 10
Y = 10
Print(x is not y, x==y)
Output: False, True
X = 5
Y = 10
Print(x is not y, x==y)
Output: True, False
Or
X = 5;
If(type(x) is int):
Print(“Correct”);
Else:
Print(“Incorrect”);
Output: Correct
Membership
Operators
Membership operators are used to test if a sequence is presented
in an object:
ये operators Data Type पर काम करते
हैं जैसे List, Tuple, Dictionary
etc. यदि list मे कोई वैल्यू या string
मौजूद है या नहीं तो ये हमको True या False
परिणाम रिटर्न करता है।
Operator |
Description |
Example |
in |
Returns True if a sequence with the specified value is present in the
object |
x in y |
not in |
Returns True if a sequence with the specified value is not present in
the object |
x not in y |
Example:-
X = “Hello”
Print(‘h’ in x)
Output: False
X = “Hello”
Print(‘H’ in x)
Output: True
Or
X = 10;
List = [1,2,3,4,5];
If (x in list):
Print(“Yes
x is present in the list”);
Else:
Print
(“No x is not present in the list”);
Output: No x is not
present in the list
X = 10;
List = [1,2,3,4,5,10];
If (x in list):
Print(“Yes
x is present in the list”);
Else:
Print
(“No x is not present in the list”);
Output: Yes x is
present in the list
Post a Comment