Understanding Identity & Membership Operators in Python

PYTHON JOURNEY - Day 10 / 50 TOPIC : Identity & Membership Operators in Python In Python, we often check who something is or what it contains. That’s where Identity and Membership operators come in --- 1. Identity Operators (is, is not) Used to check whether two variables point to the same object in memory. x = [1, 2, 3] y = x z = [1, 2, 3] print(x is y) # True (same memory) print(x is z) # False (different objects) print(x is not z) # True Tip: is compares identity, not value. Use == to compare values. --- 2. Membership Operators (in, not in) Used to check if a value exists in a sequence (like list, tuple, or string). fruits = ["apple", "banana", "cherry"] print("apple" in fruits) # True print("grape" not in fruits) # True --- Output: True False True True --- Quick Tip: Use is to check identity (like “same person”). Use in to check membership (like “is inside a group”). --- #Python #LearnPython #PythonForBeginners #PythonCoding #PythonOperators #LinkedInLearning

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories