Python Membership Operators
Membership Operators in Python.
Membership operators are used to determine whether the iterator or the value given is present in the sequence. Such operator types often return values in either the TRUE or FALSE format. If the value is contained in a set, the operator returns TRUE, otherwise it returns FALSE.
Membership Operators in Python
Membership operators in Python are used to test whether a value or element is a member of a sequence, such as a string, list, tuple, or set. These operators return True or False based on the presence or absence of the specified value within the sequence. All Membership operators in python are discussed below in detail with their syntax and proper example:
1. In
It is used check whether a variable is present in a sequence or not. It returns True if variable is found else it returns False.
Syntax
Variable in Sequence
Example
>>> a = [1,(1,2),"Prep","Insta"] >>> b = (1,2) >>> 1 in a True >>> b in a True >>> 'a' in 'PrepInsta' True
2. Not in
It is used check whether a variable is present in a sequence or not. It returns True if variable is not found else it returns False.
Syntax
Variable not in Sequence
Example
>>> a = [1,(1,2),"Prep","Insta"] >>> b = (1,2,3) >>> 2 not in a True >>> b not in a True >>> 'A' not in 'PrepInsta' True
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment