The following program demonstrates how to Search a tuple in Python.
Here’s a program to search for a value in a tuple in Python.
# Create a tuple of numbers
numbers = (1, 2, 3, 4, 5)
# The value to search for
search_value = 3
# Use a for loop to search for the value in the tuple
found = False
for number in numbers:
if number == search_value:
found = True
break
# Print the result
if found:
print("Value found in the tuple.")
else:
print("Value not found in the tuple.")
In this program, we first create a tuple of numbers. Then, we define a value to search for search_value
. We use a for loop to iterate over the numbers in the tuple and check if each number is equal to the search_value
using the equality operator (==
). If the value is found, we set the found
flag to True
and break the loop. Finally, we check the value of the found
flag and print a message indicating whether the value was found in the tuple.
Further Reading
Examples of OpenCV Library in Python
A Brief Introduction of Pandas Library in Python