Nearby lessons

41 of 108

Python - Membership Operators

Introduction

Membership operators are used to check whether a value is present in a sequence or not.

The result of a membership operation is always a Boolean value:

  • True
  • False

Membership Operators List

Operator Meaning
in Returns True if the value is present.
not in Returns True if the value is not present.

Supported Data Types

Membership operators can be used with:

  • Strings
  • Lists
  • Tuples
  • Sets
  • Dictionaries

1. in Operator

The in operator checks whether a value exists in a sequence.

Example - in Operator with String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Explanation

The character 'p' is present in the string, so the result is True.

The character 'z' is not present, so the result is False.

Example - in Operator with List

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Example - in Operator with Tuple

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Example - in Operator with Set

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Example - in Operator with Dictionary

For dictionaries, membership operators check keys only.

Example - Dictionary Membership

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Explanation

Key Value
100 durga
200 ravi

The value 100 is a key, so the result is True.

The value "durga" is not a key; it is a value, so the result is False.

2. not in Operator

The not in operator checks whether a value is absent from a sequence.

Example - not in with String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Example - not in with List

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Example - not in with Tuple

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Example - not in with Set

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Example - not in with Dictionary

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
True

Why is the Second Output True?

Dictionary membership operators check only keys.

Since "python" is a value and not a key, the result is True.

Membership Operators with User Input

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Enter Name: Rahul
True

Using Membership Operators in Conditions

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Character Found

Example - Using not in

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Access Allowed

Membership Operators with Strings

For strings, membership operators check whether a substring exists.

Example - String Membership

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Important Notes

  • Membership operators check whether a value exists in a sequence.
  • The result is always True or False.
  • Dictionary membership operators check only keys.
  • Membership operators work with strings, lists, tuples, sets, and dictionaries.

Real World Usage

Membership operators are commonly used in:

  • Login systems
  • Search operations
  • Validation
  • Filtering
  • Permission checking

Example Areas

Usage Example
Login Validation Username check
Search Word search
Access Control Permission validation

Difference Between in and not in

Operator Meaning
in Checks whether a value exists.
not in Checks whether a value does not exist.

Quick Summary

Operator Purpose
in Checks whether a value is present.
not in Checks whether a value is absent.

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Conditional Statement