Last Updated on March 10, 2023 by Prepbytes
In Operator in Python comes under the category of Membership Operators. The Membership Operators check whether the sequence appears in the object or not. So, in Operator in Python is an important operator as it can be used in a variety of scenarios, including searching for specific values, filtering elements based on certain conditions, and checking for membership in a set or dictionary.
In Operator in Python
In Python, the in operator is used to check whether a given element exists in a sequence or not. The in operator can be used in conditional statements, loops, and other places where you need to check if a particular element is present in a sequence.
Syntax of In Operator in Python
The syntax of in operator in Python is:
element in sequence
Here, the “element” is the value that you want to search for in the sequence. The “sequence” is the list, tuple, string, set, or dictionary in which you want to search for the element.
Return Type of In Operator in Python
The in operator in Python returns a boolean value of True if the element is found in the sequence, and False if it is not found.
Examples of In Operator in Python
In this section, we will look at some examples of using the in operator in Python with different sequences.
Example 1: Using in Operator in Python with Lists
Let’s start with an example of using the in operator with lists. The following example demonstrates how to check if an element is present in a list using the in operator in Python:
Code:
# Defining List fruits = ['apple', 'banana', 'cherry', 'orange'] # Using in Operator if 'apple' in fruits: print("Yes, 'apple' is in the fruits list") else: print("No, 'apple' is not in the fruits list") # Using in Operator if 'kiwi' in fruits: print("Yes, 'kiwi' is in the fruits list") else: print("No, 'kiwi' is not in the fruits list")
Output:
Yes, 'apple' is in the fruits list
No, 'kiwi' is not in the fruits list
Explanation:
In the above example, we have a list of fruits, and we are checking whether ‘apple’ is present in the list or not. Since ‘apple’ is present in the list, the output is "Yes, ‘apple’ is in the fruits list". Further, we are checking whether ‘kiwi’ is present in the fruits list or not. Since ‘kiwi’ is not present in the list, the output is "No, ‘kiwi’ is not in the fruits list".
Example 2: Using in Operator in Python with Tuples
Now, let’s look at an example of using the in operator with tuples. The following example demonstrates how to check if an element is present in a tuple using the in operator in Python.
Code:
fruits = ('apple', 'banana', 'cherry', 'orange') if 'apple' in fruits: print("Yes, 'apple' is in the fruits tuple") else: print("No, 'apple' is not in the fruits tuple")
Output:
Yes, 'apple' is in the fruits tuple
Explanation:
In the above example, we have a tuple of fruits, and we are checking whether ‘apple’ is present in the fruits tuple or not. Since ‘apple’ is present in the tuple, the output is "Yes, ‘apple’ is in the fruits tuple".
Similarly, we can check for the presence of other elements in the tuple.
Example 3: Using in Operator in Python with Strings
Now, let’s look at an example of using the in operator with strings. The following example demonstrates how to check if a character or substring is present in a string using the in operator in Python.
Code:
str1 = "Hello, World!" if 'H' in str1: print("Yes, 'H' is present in the string") else: print("No, 'H' is not present in the string")
Output:
Yes, 'H' is present in the string
Explanation:
In the above example, we are checking whether the character ‘H’ is present in the string ‘Hello, World!’. Since ‘H’ is present in the string, the output is "Yes, ‘H’ is present in the string".
Similarly, we can check for the presence of a substring in a string
Example 4: Using in Operator in Python with Sets
Now, let’s look at an example of using the in operator with sets. The following example demonstrates how to check if an element is present in a set using the in operator in Python.
Code:
fruits = {'apple', 'banana', 'cherry', 'orange'} if 'apple' in fruits: print("Yes, 'apple' is in the fruits set") else: print("No, 'apple' is not in the fruits set")
Output:
Yes, 'apple' is in the fruits set
Explanation:
In the above example, we have a set of fruits, and we are checking whether ‘apple’ is present in the set or not. Since ‘apple’ is present in the set, so we get the expected output on the screen.
Conclusion
In conclusion, the in operator in Python is a useful tool for working with iterable objects such as lists, tuples, sets, and dictionaries. It allows you to check if an element is present in the given iterable or not. One of the advantages of using the in operator is that it simplifies code and makes it more readable.
Overall, the in Operator in Python is a powerful and versatile tool in Python that can be used in a variety of scenarios. Whether you are searching for specific values, filtering elements based on certain conditions, or checking for membership in a set or dictionary, the in operator can help you write code that is efficient, readable, and maintainable.
Frequently Asked Questions
Some Frequently Asked Questions on in Operator in Python are given below.
Ques 1. Can I use the in operator to check if a string contains a substring in Python?
Ans. Yes, you can use the in operator to check if a string contains a substring in Python. For example, ‘hello’ in ‘hello world’ would return True.
Ques 2. Is the in operator in Python case-sensitive?
Ans. Yes, the in operator in Python is case-sensitive, so ‘a’ in ‘ABC’ would return False, while ‘A’ in ‘ABC’ would return True.
Ques 3. Can I use the in operator to check if a list contains multiple values at once in Python?
Ans. Yes, you can use the in operator to check if a list contains multiple values at once in Python, by chaining multiple in operators together with the and keyword. For example, “1 in my_list and 2 in my_list and 3 in my_list” would return True if my_list contains all three values. Alternatively, you can use all function to simplify this expression, like this: “all(x in my_list for x in [1, 2, 3])”.
Ques 4. Can I use the in operator to check if a list does not contain a value in Python?
Ans. Yes, you can use the not in operator to check if a value is not present in a list.