Example:
tuple = ("python", "includehelp", 43, 54.23)
Checking if the element is present in tuple
In this article, we are given a tuple and an element. Our task is to create a python program to check if the given element is present in the tuple.
Input:
(4, 1, 7, 8, 2)
ele = 4
Output:
present
For this, we need to perform a searching algorithm on elements of the tuple. And find the presence of the given element. This can be easily done in Python using one of the multiple methods provided.
Method 1:
One method to check for the presence of the given element is by simply iterating over the tuple and comparing each element with the given element. Break if an element from tuple is matched, otherwise continue.
Output:
Method 2:
Another method to solve the problem is by using Python's built-in in operator. This operator checks for the presence of the given element in the tuple and returns a boolean value for the result.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer