Python Program To Check Whether Element Present In Set Or Not Example
Chapter:
Python
Last Updated:
04-10-2023 18:01:58 UTC
Program:
/* ............... START ............... */
# Create a set
my_set = {1, 2, 3, 4, 5}
# Element to check
element_to_check = 3
# Check if the element is present in the set
if element_to_check in my_set:
print(f"{element_to_check} is present in the set.")
else:
print(f"{element_to_check} is not present in the set.")
/* ............... END ............... */
Output
3 is present in the set.
In this example, since the element 3 is present in the set my_set, the program prints "3 is present in the
set." If you were to change the value of element_to_check to something not in the set, it would print
a message indicating that it is not present.
Notes:
-
In this example, we first create a set called my_set with some elements. Then, we define the element_to_check variable and use the in keyword to check if it exists in the set. Depending on the result, we print a corresponding message.
- You can replace element_to_check with any value you want to check for in the set.