“in” Keyword in Python: Testing Membership in Iterables

"in" Keyword in Python: Testing Membership in Iterables

Testing membership in a sequence or container is a common operation in Python. Python’s “in” keyword is used to test membership in an iterable. In this article, we will explore the functionality and usage of the “in” keyword in Python.

What is an iterable?

An iterable is any Python object that can return its elements one at a time. Sequences like strings, lists, and tuples are examples of iterable objects.

Usage

The “in” keyword allows you to check if a value is present in a container or an iterable. It returns True if the value is present, otherwise it returns False.

Example 1:

# Checking if a value is present in a list

fruits = ['apple', 'banana', 'cherry']

if 'banana' in fruits:
    print("Yes, banana is a fruit!")

Output:

Yes, banana is a fruit!

In the above example, we have a list of fruits and we are checking if ‘banana’ is present in it. Since it is present, the output will be “Yes, banana is a fruit!”

Example 2:

# Checking if a value is present in a string

string = "Welcome to Python"

if 'Python' in string:
    print("Substring found!")

Output:

Substring found!

In the above example, we are checking if the substring ‘Python’ is present in a string. Since it is present, the output will be “Substring found!”

Example 3:

# Checking if a key is present in a dictionary

employee = {1: 'John', 2: 'Jane', 3: 'Bob'}

if 2 in employee:
    print("Employee ID 2 is present in the dictionary!")

Output:

Employee ID 2 is present in the dictionary!

In the above example, we have a dictionary of employees with their IDs as keys. We are checking if the key ‘2’ is present in the dictionary. Since it is present, the output will be “Employee ID 2 is present in the dictionary!”

Example 4:

# Checking if a value is not present in a list

fruits = ['apple', 'banana', 'cherry']

if 'orange' not in fruits:
    print("No, orange is not a fruit!")

Output:

No, orange is not a fruit!

In the above example, we are checking if the value ‘orange’ is not present in a list of fruits. Since it is not present, the output will be “No, orange is not a fruit!”

Example 5:

# Checking if a substring is not present in a string

string = "Welcome to Python"

if 'Java' not in string:
    print("Substring not found!")

Output:

Substring not found!

In the above example, we are checking if the substring ‘Java’ is not present in a string. Since it is not present, the output will be “Substring not found!”

Example 6:

# Checking if a key is not present in a dictionary

employee = {1: 'John', 2: 'Jane', 3: 'Bob'}

if 4 not in employee:
    print("Employee ID 4 is not present in the dictionary!")

Output:

Employee ID 4 is not present in the dictionary!

In the above example, we are checking if the key ‘4’ is not present in a dictionary of employees. Since it is not present, the output will be “Employee ID 4 is not present in the dictionary!”

Conclusion

The “in” keyword is a powerful and useful way to test membership in an iterable in Python. It can be used with strings, lists, tuples, dictionaries, and any other kind of iterable object. “in” keyword is a very simple and effective way to check if a value is present or not in a container or an iterable.

Leave a Reply

Your email address will not be published. Required fields are marked *