Membership Test Operators in Python

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 24:- Membership Test Operators in Python

In Python, membership test operators are used to check if a value exists within a collection, such as a list, tuple, set, or string. These operators allow you to determine if a particular element is present in the given collection. There are two membership test operators in Python:

  1. in operator: The in operator returns True if the specified value is present in the collection, and False otherwise.

Example:

pythonCopy code

# List membership test numbers = [1, 2, 3, 4, 5] print(3 in numbers)  # Output: True print(6 in numbers)  # Output: False # String membership test text = "Hello, World!" print("Hello" in text)   # Output: True print("Python" in text)  # Output: False

In this example, the in operator is used to check if the values 3 and 6 are present in the list numbers, and the values "Hello" and "Python" are present in the string text.

  1. not in operator: The not in operator is the negation of the in operator. It returns True if the specified value is not present in the collection, and False if the value is present.

Example:

pythonCopy code

# Tuple membership test fruits = ("apple", "banana", "orange") print("apple" not in fruits)   # Output: False print("grape" not in fruits)   # Output: True

In this example, the not in operator is used to check if the values "apple" and "grape" are present in the tuple fruits.

Membership test operators are commonly used to check for the existence of specific elements in collections before performing certain actions or making decisions based on their presence. They are particularly useful when dealing with lists, tuples, sets, and strings in Python. 

4. Operators

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?