Python Membership Operators
Python membership operators check whether the given operand is present in the sequence/series type object(list, tuple, etc.). It returns the True value if the operand is present in that sequence else, it returns False. in and not in are the two membership operators provided by Python.
Operators | Operations | Examples |
---|---|---|
in | Results in True if the left operand is present in the right operand(sequence). Else it return False. | 'Mango' in ['Apple','Watermelon','Orange','Mango'] (returns True) 'Mango' in ('Apple','Watermelon','Orange','Banana') (returns False) 'hi' in 'hello hi how do you do?' (returns True) |
not in | Results in True if the left operand is not present in the right sequence. Else it returns False. | 'Mango' not in ['Apple','Watermelon','Orange','Mango'] (returns False) 'Mango' not in ('Apple','Watermelon','Orange','Banana') (returns True) 'hi' not in 'hello hi how do you do?' (returns False) |
Identity Operators
Identity operators took two operands and determine whether these two variables point to the exact memory location or not. They can also check if a variable is of a particular data type or not. Python provides two Identity Operators: is and is not.
Operators | Operations | Examples |
---|---|---|
is | Returns True if the left operand points to the same memory location as the right operand. Returns False if not. | A=5 B=5 A is B(returns True) A=2 B=22 A is B (returns False) A = 51 type(A) is int (returns True) |
is not | Returns True if the left operand does not result in the same memory location as the right operand.Else returns False. | A=5 B=5 A is not B(returns False) A=2 B=22 A is not B (returns True) A = 51 type(A) is not int (returns False) |
Note that Python Identity operators check the memory location of the operands and not their values like the '==' comparison operator.