Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
131 views
in Python by (128k points)
How do I check if an element exists in a tuple in Python?

Please log in or register to answer this question.

1 Answer

0 votes
by (128k points)

To check if an element exists in a tuple in Python, you can use the in operator. The in operator returns True if the specified element is found in the tuple and False otherwise. 

Here's an example:

my_tuple = (1, 2, 3, 4, 5)
if 3 in my_tuple:
    print("3 is in the tuple")
else:
    print("3 is not in the tuple")
 

In this example, we're checking if the element 3 is in the my_tuple tuple. Since 3 is in the tuple, the if statement will evaluate to True and the output will be "3 is in the tuple".

Note that if you want to check if an element does not exist in a tuple, you can use the not in operator. The not in operator returns True if the specified element is not found in the tuple and False otherwise. 

Here's an example:

my_tuple = (1, 2, 3, 4, 5)
if 6 not in my_tuple:
    print("6 is not in the tuple")
else:
    print("6 is in the tuple")
 

In this example, we're checking if the element 6 is not in the my_tuple tuple. Since 6 is not in the tuple, the if statement will evaluate to True and the output will be "6 is not in the tuple".

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...