Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Difference between != and is not operator in Python
Next article icon

Python Membership and Identity Operators

Last Updated : 13 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

There is a large set of Python operators that can be used on different datatypes. Sometimes we need to know if a value belongs to a particular set. This can be done by using the Membership and Identity Operators. In this article, we will learn about Python Membership and Identity Operators.

Python Membership Operators

The Python membership operators test for the membership of an object in a sequence, such as strings, lists, or tuples. Python offers two membership operators to check or validate the membership of a value. They are as follows:

Python IN Operator

The in operator is used to check if a character/substring/element exists in a sequence or not. Evaluate to True if it finds the specified element in a sequence otherwise False.

Python
# initialized some sequences list1 = [1, 2, 3, 4, 5] str1 = "Hello World" dict1 = {1: "Geeks", 2:"for", 3:"geeks"}  # using membership 'in' operator # checking an integer in a list print(2 in list1)  # checking a character in a string print('O' in str1)   # checking for a key in a dictionary print(3 in dict1) 

Output
True False True 

Python NOT IN Operator

The ‘not in’ Python operator evaluates to true if it does not find the variable in the specified sequence and false otherwise.

Python
# initialized some sequences list1 = [1, 2, 3, 4, 5] str1 = "Hello World" dict1 = {1: "Geeks", 2:"for", 3:"geeks"}  # using membership 'not in' operator # checking an integer in a list print(2 not in list1)  # checking a character in a string print('O' not in str1)  # checking for a key in a dictionary print(3 not in dict1) 

Output
False True False 

operators.contains() Method

An alternative to Membership ‘in’ operator is the contains() function. This function is part of the Operator module in Python. The function take two arguments, the first is the sequence and the second is the value that is to be checked.

Syntax: operator.contains(sequence, value)

Example: In this code we have initialized a list, string, set, dictionary and a tuple. Then we use operator module’s contain() function to check if the element occurs in the corresponding sequences or not.

Python
# import module import operator  # using operator.contain() # checking an integer in a list print(operator.contains([1, 2, 3, 4, 5], 2))  # checking a character in a string print(operator.contains("Hello World", 'O'))  # checking an integer in aset print(operator.contains({1, 2, 3, 4, 5}, 6))  # checking for a key in a dictionary print(operator.contains({1: "Geeks", 2:"for", 3:"geeks"}, 3))  # checking for an integer in a tuple print(operator.contains((1, 2, 3, 4, 5), 9)) 

Output
True False False True False 

Python Identity Operators

The Python Identity Operators are used to compare the objects if both the objects are actually of the same data type and share the same memory location. There are different identity operators such as:

Python IS Operator

The is operator evaluates to True if the variables on either side of the operator point to the same object in the memory and false otherwise.

Python
# Python program to illustrate the use # of 'is' identity operator num1 = 5 num2 = 5  a = [1, 2, 3] b = [1, 2, 3] c = a  s1 = "hello world" s2 = "hello world"  # using 'is' identity operator on different datatypes print(num1 is num2) print(a is b) print(a is c) print(s1 is s2) print(s1 is s2) 

Output
True False True True True 

We can see here that even though both the lists, i.e., ‘lst1’ and ‘lst2’ have same data, the output is still False. This is because both the lists refers to different objects in the memory. Where as when we assign ‘lst3’ the value of ‘lst1’, it returns True. This is because we are directly giving the reference of ‘lst1’ to ‘lst3’.

Python IS NOT Operator

The is not operator evaluates True if both variables on the either side of the operator are not the same object in the memory location otherwise it evaluates False.

Python
# Python program to illustrate the use # of 'is' identity operator num1 = 5 num2 = 5  a = [1, 2, 3] b = [1, 2, 3] c = a  s1 = "hello world" s2 = "hello world"  # using 'is not' identity operator on different datatypes print(num1 is not num2) print(a is not b) print(a is not c) print(s1 is not s2) print(s1 is not s1) 

Output
False True False False False 

Difference between ‘==’ and ‘is’ Operator

While comparing objects in Python, the users often gets confused between the Equality operator and Identity ‘is’ operator. The equality operator is used to compare the value of two variables, whereas the identities operator is used to compare the memory location of two variables. Let us see the difference with the help of an example.

Example: In this code we have two lists that contains same data. The we used the identity ‘is’ operator and equality ‘==’ operator to compare both the lists.

Python
# Python program to illustrate the use # of 'is' and '==' operators a = [1, 2, 3] b = [1, 2, 3]  # using 'is' and '==' operators print(a is b) print(a == b) 

Output
False True 

Read in detail – Difference between == and is operator



Next Article
Difference between != and is not operator in Python

P

Pushpanjali chauhan
Improve
Article Tags :
  • Python
  • Python-Operators
Practice Tags :
  • python
  • python-operators

Similar Reads

  • Python Operators
    In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. OPERATORS: These are the special symbols. Eg- + , * , /,
    6 min read
  • Precedence and Associativity of Operators in Python
    In Python, operators have different levels of precedence, which determine the order in which they are evaluated. When multiple operators are present in an expression, the ones with higher precedence are evaluated first. In the case of operators with the same precedence, their associativity comes int
    4 min read
  • Python Arithmetic Operators

    • Python Arithmetic Operators
      Python operators are fundamental for performing mathematical calculations. Arithmetic operators are symbols used to perform mathematical operations on numerical values. Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). OperatorDescriptionS
      5 min read
    • Difference between / vs. // operator in Python
      In Python, both / and // are used for division, but they behave quite differently. Let's dive into what they do and how they differ with simple examples. / Operator (True Division)The / operator performs true division.It always returns a floating-point number (even if the result is a whole number).I
      2 min read
    • Python - Star or Asterisk operator ( * )
      The asterisk (*) operator in Python is a versatile tool used in various contexts. It is commonly used for multiplication, unpacking iterables, defining variable-length arguments in functions, and more. Uses of the asterisk ( * ) operator in PythonMultiplicationIn Multiplication, we multiply two numb
      3 min read
    • What does the Double Star operator mean in Python?
      The ** (double star)operator in Python is used for exponentiation. It raises the number on the left to the power of the number on the right. For example: 2 ** 3 returns 8 (since 2³ = 8)It is one of the Arithmetic Operator (Like +, -, *, **, /, //, %) in Python and is also known as Power Operator. Pr
      2 min read
    • Division Operators in Python
      Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. There are two types of division operators: Float divisionInteger division( Floor division)When an in
      5 min read
    • Modulo operator (%) in Python
      Modulo operator (%) in Python gives the remainder when one number is divided by another. Python allows both integers and floats as operands, unlike some other languages. It follows the Euclidean division rule, meaning the remainder always has the same sign as the divisor. It is used in finding even/
      4 min read
    • Python Logical Operators

      geeksforgeeks-footer-logo
      Corporate & Communications Address:
      A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
      Registered Address:
      K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
      GFG App on Play Store GFG App on App Store
      Advertise with us
      • Company
      • About Us
      • Legal
      • Privacy Policy
      • In Media
      • Contact Us
      • Advertise with us
      • GFG Corporate Solution
      • Placement Training Program
      • Languages
      • Python
      • Java
      • C++
      • PHP
      • GoLang
      • SQL
      • R Language
      • Android Tutorial
      • Tutorials Archive
      • DSA
      • Data Structures
      • Algorithms
      • DSA for Beginners
      • Basic DSA Problems
      • DSA Roadmap
      • Top 100 DSA Interview Problems
      • DSA Roadmap by Sandeep Jain
      • All Cheat Sheets
      • Data Science & ML
      • Data Science With Python
      • Data Science For Beginner
      • Machine Learning
      • ML Maths
      • Data Visualisation
      • Pandas
      • NumPy
      • NLP
      • Deep Learning
      • Web Technologies
      • HTML
      • CSS
      • JavaScript
      • TypeScript
      • ReactJS
      • NextJS
      • Bootstrap
      • Web Design
      • Python Tutorial
      • Python Programming Examples
      • Python Projects
      • Python Tkinter
      • Python Web Scraping
      • OpenCV Tutorial
      • Python Interview Question
      • Django
      • Computer Science
      • Operating Systems
      • Computer Network
      • Database Management System
      • Software Engineering
      • Digital Logic Design
      • Engineering Maths
      • Software Development
      • Software Testing
      • DevOps
      • Git
      • Linux
      • AWS
      • Docker
      • Kubernetes
      • Azure
      • GCP
      • DevOps Roadmap
      • System Design
      • High Level Design
      • Low Level Design
      • UML Diagrams
      • Interview Guide
      • Design Patterns
      • OOAD
      • System Design Bootcamp
      • Interview Questions
      • Inteview Preparation
      • Competitive Programming
      • Top DS or Algo for CP
      • Company-Wise Recruitment Process
      • Company-Wise Preparation
      • Aptitude Preparation
      • Puzzles
      • School Subjects
      • Mathematics
      • Physics
      • Chemistry
      • Biology
      • Social Science
      • English Grammar
      • Commerce
      • World GK
      • GeeksforGeeks Videos
      • DSA
      • Python
      • Java
      • C++
      • Web Development
      • Data Science
      • CS Subjects
      @GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
      We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
      Lightbox
      Improvement
      Suggest Changes
      Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
      geeksforgeeks-suggest-icon
      Create Improvement
      Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
      geeksforgeeks-improvement-icon
      Suggest Changes
      min 4 words, max Words Limit:1000

      Thank You!

      Your suggestions are valuable to us.

      What kind of Experience do you want to share?

      Interview Experiences
      Admission Experiences
      Career Journeys
      Work Experiences
      Campus Experiences
      Competitive Exam Experiences