Account
Categories

Python Tuple Membership (in / not in)


Definition

It checks whether the data is present in a tuple using the membership operators (in / not in).

Whereas:

The "in" operator checks if the data is present in the tuple and returns True, otherwise False.

The "not in" operator checks if the data is not present in the tuple and returns True, otherwise False.

Now you will clearly understand the operation through the flowchart.

Tuple →  t = (11, 20, 30)

Check 1:
20 ───► t
       (11, 20, 30)

✔ Found →  True   (because 20 is present)

----------------------------

Check 2:
40 ───► t
       (11, 20, 30)

❌ Not Found →  True   (because 40 is not present)

Syntax:

value in tuple
value not in tuple

Example:

t = (11, 20, 30)

print(20 in t)
print(40 not in t)

Output:

Exercise

Create a tuple t = (5, 10, 15) and check whether 10 is present and 20 is not present.