Using in and not in Operators in Python

The in and not in operators in Python are used to check if an element exists within a sequence or collection, such as a string, list, tuple, dictionary, or set. These operators return a boolean value, True or False, based on whether the element is present or absent.

1. in Operator

The in operator checks if a specific element exists within a collection. If the element is found, it returns True; otherwise, it returns False.

Code Example for in Operator

# Using the 'in' operator with different collections
        
        # Example with a string
        sentence = "Python programming"
        print("'Python' in sentence:", 'Python' in sentence)
        
        # Example with a list
        numbers = [1, 2, 3, 4, 5]
        print("3 in numbers:", 3 in numbers)
        
        # Example with a dictionary (checks keys)
        student_info = {'name': 'Alice', 'age': 21}
        print("'name' in student_info:", 'name' in student_info)

Output

'Python' in sentence: True
3 in numbers: True
'name' in student_info: True

Explanation

Here, we use the in operator to check for:

---

2. not in Operator

The not in operator checks if an element does not exist within a collection. If the element is absent, it returns True; otherwise, it returns False.

Code Example for not in Operator

# Using the 'not in' operator with different collections
        
        # Example with a string
        word = "hello"
        print("'a' not in word:", 'a' not in word)
        
        # Example with a list
        fruits = ["apple", "banana", "cherry"]
        print("'orange' not in fruits:", 'orange' not in fruits)
        
        # Example with a dictionary (checks keys)
        person = {'name': 'Bob', 'age': 30}
        print("'address' not in person:", 'address' not in person)

Output

'a' not in word: True
'orange' not in fruits: True
'address' not in person: True

Explanation

Here, we use the not in operator to check if:

---

Examples of Using in and not in Operators

Example: Filtering Elements in a List

You can use the in and not in operators to filter elements in a list based on a condition.

Code Example

# Filtering elements in a list using 'in' and 'not in'
        
        # List of all available colors
        available_colors = ["red", "blue", "green", "yellow"]
        
        # List of colors to be excluded
        excluded_colors = ["blue", "yellow"]
        
        # Filtered list with only colors not in excluded_colors
        filtered_colors = [color for color in available_colors if color not in excluded_colors]
        print("Filtered colors:", filtered_colors)

Output

Filtered colors: ['red', 'green']

Explanation

This example filters out colors that are in excluded_colors from the available_colors list. The not in operator helps remove unwanted elements.

Conclusion

The in and not in operators are essential tools in Python for membership testing within collections. They provide a simple way to check for the presence or absence of elements, making it easier to filter, validate, and work with collections in an efficient and readable manner.