Write a Python Program to Check Whether an Element Exists Within a Tuple

In this article let's understand how to write a Python program to check whether a  given item exists within the tuple or not.

The below program program we will be asking a user to enter the tuple and then iterate all the items present in a tuple

 

Write a Python Program to Check Whether an Element Exists Within a Tuple

 



Write a Python Program to Check Whether an Element Exists Within a Tuple

 Let's take a variable n and and use it to store the integer variable that is tuple length,  using the input function call to ask a user to enter the tuple length and then convert it into an integer using the type conversion.

Iterate from 0 to to tuple length Using the for loop ask a user to enter the tuple item and append it to a tuple using Plus “+”.

n = int(input("Enter Tuple Length : "))
numbers = []
for _ in range(n):
  temp = int(input("Enter List Item (Integer) : "))
  numbers += (temp,)



Using the input function call ask a user to enter the item to search,  store the item into a variable named item by converting it to an integer
item = int(input("Enter Tuple Item to Search : "))




Let's use the if statement and in keyword to check if an item is present in a tuple containing the numbers.  if the the statement is true then print item is present in the tuple else print the item is not present in the tuple


if item in numbers:
   print("Item Present in Tuple")
else:
  print("Item Not Present in Tuple")





Complete Python Program
========================

n = int(input("Enter Tuple Length : "))
numbers = []
for _ in range(n):
 temp = int(input("Enter List Item (Integer) : "))
 numbers += (temp,)


item = int(input("Enter Tuple Item to Search : "))

if item in numbers:
   print("Item Present in Tuple")
else:
  print("Item Not Present in Tuple")





Output
===============
Enter Tuple Length : 12
Enter List Item (Integer) : 1
Enter List Item (Integer) : 2
Enter List Item (Integer) : 3
Enter List Item (Integer) : 4
Enter List Item (Integer) : 5
Enter List Item (Integer) : 6
Enter List Item (Integer) : 7
Enter List Item (Integer) : 8
Enter List Item (Integer) : 9
Enter List Item (Integer) : 10
Enter List Item (Integer) : 11
Enter List Item (Integer) : 12
Enter Tuple Item to Search : 55
Item Not Present in Tuple




Output
===============
Enter Tuple Length : 5
Enter List Item (Integer) : 1
Enter List Item (Integer) : 2
Enter List Item (Integer) : 3
Enter List Item (Integer) : 4
Enter List Item (Integer) : 5
Enter Tuple Item to Search : 3
Item Present in Tuple




Conclusion
==============
You can also declare the input tuple inside your Python program and keep searching the item present in tuples.

The above program is very simple,  if you you are looking for program to check if element is present in nested tuples then below is the link  for the Python program

https://codewithtj.blogspot.com/2022/11/write-python-program-check-specified-element-presents-tuple-tuples.html




Post a Comment

0 Comments