how to check if an element is in a set in python

In Python, we can check if an element is present in a set using the "in" operator. This operator returns a boolean value of "True" if the specified element is present in the given set, else "False".

Here's an example of how to check if an element is in a set:

main.py
my_set = {1, 2, 3, 4, 5}
print(3 in my_set)   # Output: True
print(6 in my_set)   # Output: False
98 chars
4 lines

In the above example, we are checking whether the elements '3' and '6' are present in the set 'my_set'. The first print statement returns "True" as '3' is present in the set, whereas the second print statement returns "False" as '6' is not present in the set.

gistlibby LogSnag