Skip to content

Python set contains | Example code

  • by

Use the in keyword to check set contains a given element or not in Python. If in statement returns True if the specified set contains an element and False otherwise.

You have to use “in Keyword” with the if statement.

if ele in seq:
    statement(s)

Note: Its case sensitive means uppercase and lowercase matter to check the element present in the set.

Example Check If Set Contains an Element in Python

Simple example code.

fruits = {"Apple", "Banana", "Cherry"}

if "Apple" in fruits:
    print("Yes, 'Apple' is in fruits set")

Output:

Python set contains

Another example

Check if 1 is in a_set

a_set = {1, 2, 3}

res = 1 in a_set

print(res)

Output: True

Do comment if you have any doubts or suggestions on this Python set topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading