w3resource

Python Exercise: Check whether an element exists within a tuple


10. Check Whether an Element Exists Within a Tuple

Write a Python program to check whether an element exists within a tuple.

Visual Presentation:

Python Tuple: Check whether an element exists within a tuple.

Sample Solution:

Python Code:

# Create a tuple containing a sequence of items
tuplex = ("w", 3, "r", "e", "s", "o", "u", "r", "c", "e")

# Check if the character "r" is present in the 'tuplex' tuple and print the result
print("r" in tuplex)

# Check if the number 5 is present in the 'tuplex' tuple and print the result
print(5 in tuplex)

Sample Output:

True                                                                                                          
False 

Flowchart:

Flowchart: Check whether an element exists within a tuple

For more Practice: Solve these Related Problems:

  • Write a Python program to check if a specified element is present in a tuple using the in operator.
  • Write a Python program to implement a function that searches for an element in a tuple and returns its index if found.
  • Write a Python program to use a loop to scan through a tuple and determine whether a given element exists.
  • Write a Python program to use exception handling to return a custom message if an element is not found in the tuple.

Python Code Editor:

Previous: Write a Python program to find the repeated items of a tuple.
Next: Write a Python program to convert a list to a tuple.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.