Membership operators in Python for Data Science –
These operators are used for validating membership of a value. The membership test is done in a sequence, for example lists, tuples or strings.
Some of the membership operators are as follows:
- in operator – It is used for checking whether the existence of a value is sequential or not. It returns TRUE if the value found is in the specified sequence. In other cases, it returns FALSE.
Let’s look at the above example but this time without the use of ‘in’ operator.
- not in operator – As the name suggests, it returns TRUE if it is unable to find a variable in the defined sequence. In other cases, it returns FALSE.
These operators are useful when we have to determine whether or not a value belongs to a certain class or type. It is used to find out the type of data contained in a certain variable.
- is operator – Returns TRUE if both the variables refer to the same object otherwise returns FALSE.
- is not operator – As the name suggests, it is the opposite of ‘is’ operator. If the variables refer to different objects, it returns TRUE, otherwise returns FALSE.