Menu
Coddy logo textTech

Membership Checks

Part of the Logic & Flow section of Coddy's Python journey — lesson 25 of 78.

Membership checks in Python let you check if a value exists in a collection like a list, tuple, set, or dictionary using in and not in.

The in operator checks if a value exists:

fruits = ["apple", "banana", "cherry"]
print("apple" in fruits)  # True

The not in operator checks if a value does not exist:

numbers = [1, 2, 3]
print(4 not in numbers)  # True

For dictionaries, membership checks apply to keys by default:

my_dict = {"name": "Alice", "age": 25}
print("name" in my_dict)  # True
print("Alice" in my_dict)  # False
challenge icon

Challenge

Easy

You are given a list of names and a dictionary of student grades. Write a program that:

  1. Check if "Alice" is in the list → Print: "Alice is in the list."
  2. Check if "David" is not in the list → Print: "David is not in the list."
  3. Check if "Bob" is in the dictionary → Print: "Bob is in the dictionary."
  4. Check if "Eve" is not in the dictionary → Print: "Eve is not in the dictionary."

Cheat sheet

Use in and not in operators to check membership in collections:

Check if a value exists with in:

fruits = ["apple", "banana", "cherry"]
print("apple" in fruits)  # True

Check if a value does not exist with not in:

numbers = [1, 2, 3]
print(4 not in numbers)  # True

For dictionaries, membership checks apply to keys by default:

my_dict = {"name": "Alice", "age": 25}
print("name" in my_dict)  # True
print("Alice" in my_dict)  # False

Try it yourself

# Given data
names = ["Alice", "Bob", "Charlie"]
grades = {"Alice": 85, "Bob": 90, "Charlie": 78}

# Write code here
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow