ADVERTISEMENT
ADVERTISEMENT

Python program to check if the element is present in tuple

In this program, we are given a tuple with some values. We need to create a Python program to check if the given element is present in the tuple.
Submitted by Shivang Yadav, on December 19, 2021

Checking for the presence of an element in a collection has found many uses as searching is required in many cases. Python allows its users to perform this task effectively also there are many algorithms too that are used to perform this task. Here, we will see a Python program to check if the given element is present in the tuple.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

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.

# Python program to check if the 
# element is present in tuple

# Initializing and printing the tuple 
# and search element
myTuple = (5, 2, 7, 9, 1, 4)
print("The element of the tuple are " + str(myTuple))
ele = 9
print("The search element is " + str(ele))

# Checking for the presence of the 
# element is tuple
isPresent = False
for val in myTuple :
	if ele == val :
		isPresent = True
		break

# Printing result
if isPresent : 
    print("The element is present in the tuple")
else :
    print("The element is not present in the tuple")

Output:

The element of the tuple are (5, 2, 7, 9, 1, 4)
The search element is 9
The element is present in the tuple

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.

# Python program to check if the element 
# is present in tuple

# Initializing and printing the tuple and search element
myTuple = (5, 2, 7, 9, 1, 4)
print("The element of the tuple are " + str(myTuple))
ele = 9
print("The search element is " + str(ele))

# Checking for the presence of the element is tuple
isPresent = ele in myTuple

# Printing result
if isPresent : 
    print("The element is present in the tuple")
else :
    print("The element is not present in the tuple")

Output:

The element of the tuple are (5, 2, 7, 9, 1, 4)
The search element is 9
The element is present in the tuple

Python Tuple Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.