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
2.6k views
in Python by (114k points)
How to check if an element is in a set in Python?

Please log in or register to answer this question.

1 Answer

0 votes
by (114k points)

You can check if an element is present in a set in Python using the in keyword. The in keyword returns True if the element is present in the set, and False otherwise. 

Here's an example:

my_set = {1, 2, 3, 4, 5}
if 3 in my_set:
    print("3 is present in the set")
else:
    print("3 is not present in the set")
 

Output:

3 is present in the set
 

You can also use the not in keyword to check if an element is not present in the set. 

Here's an example:

my_set = {1, 2, 3, 4, 5}
if 6 not in my_set:
    print("6 is not present in the set")
else:
    print("6 is present in the set")
 

Output:

6 is not present in the set
 

Note that the in and not in keywords work with all types of sets, including sets of strings, sets of tuples, etc.

Related questions

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

...