Python Membership Operators Tutorial – Complete Guide

Welcome to this comprehensive tutorial on Python membership operators. Whether you’re just starting your coding journey or have some experience under your belt, you’ll find this material engaging and easy to grasp. We’ll explore the concept of membership operators in Python, why they’re important, and show practical examples to solidify your understanding.

What Are Python Membership Operators?

Python membership operators are a crucial part of the language. They help in determining whether a value is a member of a sequence, like strings, lists, or tuples. In simple terms, membership operators help us check if an element belongs to a collection.

Python’s membership operators play a vital role in handling collections and improving the efficiency of your code. They are essential for writing elegant, efficient, and easy-to-understand Python code, which is the goal of any programmer. Consequently, mastering them is a step in the right direction on your coding journey.

Python has two membership operators:

  • in: If a value is found in the sequence, the ‘in’ operator returns True, otherwise, it returns False.
  • not in: This operator returns True if a value is not found in the sequence, otherwise, it returns False. It’s the exact opposite of the in operator.

Why Should I Learn About Membership Operators?

Understanding membership operators is fundamental for any Python programmer and game creator. They are handy tools for rapidly checking the presence of an item in a collection, making your code more efficient and easy to read. Imagine having a game where you need to verify if a certain item exists within the player’s inventory. With Python membership operators, you can perform this check painlessly.

CTA Small Image

PART 2: Python Membership Operators in Action

Now that we know what membership operators are let’s dive into some examples.

‘in’ operator

Let’s start by using ‘in’ operator to check if an element exists in a sequence.

# defining a list
list = ['apple', 'banana', 'orange']

# checking if 'apple' is present
print('apple' in list) 
#Output: True

# checking if 'grapes' is present
print('grapes' in list)
#Output: False

As you can see, the ‘in’ operator returns True when the value is found in the sequence and False otherwise.

The ‘not in’ operator

The ‘not in’ operator checks if an element does not exist in a sequence. Let’s try to understand this with an example:

# defining a list
list = ['apple', 'banana', 'orange']

# checking if 'apple' is not present
print('apple' not in list) 
#Output: False

# checking if 'grapes' is not present
print('grapes' not in list)
#Output: True

The ‘not in’ operator returns True if the value is not found in the list, and False if it is found.

PART 3: More Python Membership Operators examples

Using Membership Operators with Strings

Membership operators can also be used with strings to check for the presence of a substring.

# defining a string
sentence = 'I am learning


 at Zenva

# checking if the word 'programming' is present
print('programming' in sentence)
#Output: True

# checking if the word '

game development

' is present
print('game development' in sentence) 
#Output: False

The ‘in’ operator confirms whether the substrings ‘programming’and ‘game development’ occur in the sentence. Similarly, we can use ‘not in’ operator:

# defining a string
sentence = 'I am learning programming at Zenva'

# checking if the word 'game development' is not present
print('game development' not in sentence)
#Output: True

# checking if the word 'programming' is not present
print('programming' not in sentence)
#Output: False

From the examples shown above, we can conclude that ‘game development’ does not occur in the sentence, while the substring ‘programming’ does.

PART 4: Using Membership Operators with Dictionaries

Membership operators in Python can also be used to check if a key is in a dictionary. Let’s see how!

# defining a dictionary
dictionary = {'name':'John', 'age':30, 'job':'Engineer'}

# checking if 'name' key is present
print('name' in dictionary) 
#Output: True

# checking if 'salary' key is present
print('salary' in dictionary) 
#Output: False

The ‘in’ operator checks if specific keys (‘name’ and ‘salary’) occur in the given dictionary.

Using ‘not in’ operator:

#defining a dictionary
dictionary = {'name':'John', 'age':30, 'job':'Engineer'}

# checking if 'name' key is not present
print('name' not in dictionary)
#Output: False

# checking if 'salary' key is not present
print('salary' not in dictionary)
#Output: True

With ‘not in’ operator, the keys ‘name’ and ‘salary’ are confirmed to be absent in the dictionary.

PART 5: Applying Membership Operators in Conditions

Python membership operators also come in handy when writing conditions such as those in an ‘if’ statement.

# defining a list of fruits
fruits = ['apple', 'banana', 'orange']

# Checking if 'apple' is in the list
if 'apple' in fruits:
    print('apple is in the list')
    print('apple not found in list')
#Output: apple is in the list

The ‘in’ operator here helps determine whether ‘apple’ exists in the list, implementing the response based on it.

Dealing with Cases in Python Membership Operators

Membership operators are case sensitive, which is another important aspect to remember while using them.

# defining a list of words
words = ['Hello', 'World', 'Python']

# checking if 'Hello' is present
print('Hello' in words)
#Output: True

# checking if 'hello' is present
print('hello' in words)
#Output: False

The ‘in’ operator highlights that ‘Hello’ exists in the list while ‘hello’ does not, illustrating the case-sensitive nature of Python membership operators.

Part 4: Where to Go Next?

Now that we’ve explored Python Membership Operators, you might be asking, “What’s next?”

One highly recommended resource to advance your Python learning journey is our Python Mini-Degree at Zenva Academy. The Python Mini-Degree is a comprehensive collection of courses covering numerous aspects of Python programming, from the basics to more complex concepts like algorithms, object-oriented programming, and even game development.

To put your newly acquired skills into practice, the program allows you to undertake several projects that you can proudly showcase in your portfolio. Remember, Python is a high-demand language in today’s job market, particularly in data science, so your continued learning will undoubtedly boost your career.

Alternatively, if you want to explore more individual Python courses, check out our entire catalog of Python courses at Zenva. We have an extensive range of Python courses covering diverse aspects of the language, suitable for learners of all levels –from beginners starting their coding journey to seasoned programmers looking to expand their skills.

At Zenva, we pride ourselves on providing flexible, accessible, and high-quality content to learners worldwide. Whether you want to learn coding from scratch, advance your game creation skills, or delve into AI, we’ve got you covered. Join the Zenva community and take your learning journey further towards becoming a proficient coder and game developer.

Remember, learning is a constant journey, and every new concept you master brings you one step closer to your goals. Keep striving, keep learning, and see you in the next tutorial!


Python membership operators are a quintessential aspect of Python programming, and they hold exceptional value for anyone seeking to create games using Python. As you continue to refine your coding expertise, remember that mastering these small yet powerful components can greatly enhance the efficacy and readability of your code.

At Zenva, we’re committed to guiding and supporting you through each step of your coding and game development journey. Dive deeper into Python and its applications in our comprehensive Python Mini-Degree program. Mastery is within your reach if you’re willing to learn. See you in the next tutorial!

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.