coding180 icon
Coding180
Membership Operators: "In" and "Not in": examples and exercises

Membership Operators: "In" and "Not in": examples and exercises

Home > Tutorials > Python for beginners > Operators


If you're new to programming, one of the first things you'll learn is how to check if a value is present in a sequence or collection. This is where membership operators come into play. Python has two membership operators that you can use to test if a value is in a sequence or not: in and not in.

The "in" Membership Operator

The in operator checks if a value is present in a sequence. Here's an example:

python
fruits = ["apple", "banana", "cherry"]
if "apple" in fruits:
    print("Yes, 'apple' is in the fruits list")

Output:

Yes, 'apple' is in the fruits list

In this example, we have a list of fruits and we use the in operator to check if the string "apple" is in the list. Since it is, the code block inside the if statement is executed.

You can also use the in operator to check if a character is in a string:

text = "Hello, World!"
if "o" in text:
    print("Yes, 'o' is in the text")

Output:

Yes, 'o' is in the text

 

Here, we check if the character "o" is in the string "Hello, World!". Since it is, the print() statement is executed.

The "not in" Membership Operator

The not in operator checks if a value is NOT present in a sequence. Here's an example:

fruits = ["apple", "banana", "cherry"]
if "orange" not in fruits:
    print("No, 'orange' is not in the fruits list")

Output:

No, 'orange' is not in the fruits list

In this example, we check if the string "orange" is NOT in the fruits list using the not in operator. Since it isn't, the code block inside the if statement is executed.

You can also use the not in operator to check if a character is NOT in a string:

text = "Hello, World!"
if "x" not in text:
    print("No, 'x' is not in the text")

Output:

No, 'x' is not in the text

 

Here, we check if the character "x" is NOT in the string "Hello, World!". Since it isn't, the print() statement is executed.

Exercises

Now that you understand how to use the in and not in operators, here are some exercises for you to practice:

  1. Write a program that asks the user to enter a sentence, then checks if the letter "a" is in the sentence.

  2. Create a list of your favorite foods. Write a program that asks the user to enter a food, then checks if that food is in your list.

  3. Write a program that generates a random number between 1 and 10, then asks the user to guess the number. If the user's guess is NOT in the range of 1 to 10, display an error message.

Conclusion

Membership operators are powerful tools in Python that allow you to check if a value is present in a sequence or collection. The in operator checks if a value is present, while the not in operator checks if it isn't. By mastering these operators, you'll be able to write more efficient and effective Python code.


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel