Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using Membership Operators (in, not in) in Python
📖 Scenario: You work at a library. You have a list of books currently available on the shelves. You want to check if certain books are in the library or not.
🎯 Goal: Build a small program that checks if some books are available in the library using membership operators in and not in.
📋 What You'll Learn
Create a list called library_books with exact book titles
Create a variable called check_book with a specific book title
Use in or not in to check if check_book is in library_books
Print the result as a sentence
💡 Why This Matters
🌍 Real World
Libraries, stores, and many apps need to check if an item is available in a collection before proceeding.
💼 Career
Understanding membership operators helps in filtering data, validating inputs, and controlling program flow in many programming jobs.
Progress0 / 4 steps
1
Create the list of books
Create a list called library_books with these exact book titles: 'The Hobbit', '1984', 'Pride and Prejudice', 'To Kill a Mockingbird', and 'The Great Gatsby'.
Python
Hint
Remember to use square brackets [] to create a list and separate items with commas.
2
Set the book to check
Create a variable called check_book and set it to the string '1984'.
Python
Hint
Use simple assignment with the exact string '1984'.
3
Check if the book is in the library
Use an if statement with the membership operator in to check if check_book is in library_books. Inside the if, create a variable called message and set it to "The book is available in the library.". In the else part, set message to "The book is not available in the library.".
Python
Hint
Use if check_book in library_books: to check membership.
4
Print the result message
Print the variable message to show if the book is available or not.
Python
Hint
Use print(message) to show the result.
Practice
(1/5)
1. What does the in operator do in Python?
easy
A. Creates a new collection
B. Adds a value to a collection
C. Removes a value from a collection
D. Checks if a value exists inside a collection
Solution
Step 1: Understand the purpose of in
The in operator is used to check if a value is present inside a collection like a list, string, or tuple.
Step 2: Compare with other options
Options A, B, and C describe actions unrelated to membership checking.
Final Answer:
Checks if a value exists inside a collection -> Option D
Quick Check:
in means membership check [OK]
Hint: Remember: in means 'is inside' [OK]
Common Mistakes:
Confusing in with adding or removing items
Thinking in changes the collection
Mixing in with comparison operators
2. Which of the following is the correct syntax to check if the string 'apple' is NOT in the list fruits?
easy
A. if 'apple' not in fruits:
B. if 'apple' not fruits:
C. if not 'apple' fruits:
D. if 'apple' !in fruits:
Solution
Step 1: Recall correct syntax for not in
The correct syntax to check absence is value not in collection.
Step 2: Evaluate each option
if 'apple' not in fruits: matches correct syntax. if 'apple' not fruits: misses in. if not 'apple' fruits: misses in. if 'apple' !in fruits: uses invalid operator !in.
Final Answer:
if 'apple' not in fruits: -> Option A
Quick Check:
Use not in as two words [OK]
Hint: Use not in as two words, no symbols [OK]
Common Mistakes:
Writing !in instead of not in
Omitting in after not
Using not 'value' in which is valid but less clear
3. What is the output of this code?
letters = ['a', 'b', 'c']
print('d' in letters)
print('a' not in letters)
medium
A. True\nTrue
B. False\nTrue
C. False\nFalse
D. True\nFalse
Solution
Step 1: Check if 'd' is in the list
'd' is not in ['a', 'b', 'c'], so 'd' in letters is False.
Step 2: Check if 'a' is not in the list
'a' is in the list, so 'a' not in letters is False, so print('a' not in letters) prints False.
Final Answer:
False
False is incorrect because the second print outputs False, but the first print outputs False, so the correct output is False
False which matches False\nFalse.
Quick Check:
'd' in letters = False, 'a' not in letters = False [OK]
Hint: Check each membership separately [OK]
Common Mistakes:
Assuming 'd' is in the list
Mixing up in and not in results
Confusing True/False outputs
4. Find the error in this code snippet:
items = ['pen', 'pencil', 'eraser']
if 'pen' not items:
print('Pen is missing')
medium
A. Incorrect print statement syntax
B. Wrong list name
C. Missing in after not
D. No error, code is correct
Solution
Step 1: Check the membership syntax
The correct syntax for checking absence is value not in collection. The code misses in after not.
Step 2: Verify other parts
List name and print statement are correct. The only error is missing in.
Final Answer:
Missing in after not -> Option C
Quick Check:
Use not in together [OK]
Hint: Always write not in together [OK]
Common Mistakes:
Writing not items instead of not in items
Forgetting in keyword
Assuming not alone checks membership
5. You have a list words = ['cat', 'dog', 'bird']. You want to print all words that are NOT in the string text = 'I have a dog and a cat'. Which code correctly does this?
hard
A. for w in words:
if w not in text:
print(w)
B. for w in words:
if w in text:
print(w)
C. for w in words:
if not w text:
print(w)
D. for w in words:
if w not text:
print(w)
Solution
Step 1: Understand the goal
We want to print words from the list that are NOT found inside the string text.
Step 2: Check each option's logic
for w in words:
if w not in text:
print(w) uses if w not in text, which correctly checks absence. for w in words:
if w in text:
print(w) prints words that ARE in text, opposite of goal. for w in words:
if not w text:
print(w) has syntax error, missing in after w. for w in words:
if w not text:
print(w) has syntax error missing in.
Final Answer:
for w in words:
if w not in text:
print(w) -> Option A
Quick Check:
Use not in for absence check [OK]
Hint: Use if w not in text to find missing words [OK]