script_16.py - List Membership Checker
Code
#!/usr/bin/env python3
"""List membership checker"""
favorite_colors = ["red", "blue", "green", "yellow"]
color = input("Enter a color: ").lower()
if color in favorite_colors:
print(f"{color} is in the favorite colors list")
else:
print(f"{color} is not in the favorite colors list")
Explanation
Line 4: favorite_colors = ["red", "blue", "green", "yellow"]
- [] syntax creates a list (mutable sequence type)
- List contains string elements
- favorite_colors variable is type list
- Lists can hold multiple values of any type
- Elements are ordered and accessible by index
Line 5: color = input("Enter a color: ").lower()
- input() returns string
- .lower() method chaining converts to lowercase
- Makes comparison case-insensitive
- color stores lowercase string
Line 7: if color in favorite_colors:
- in is membership operator for sequences (lists, strings, tuples)
- Checks if color exists anywhere in the list
- Returns boolean (True if found, False if not found)
- Iterates through list comparing each element
- Case-sensitive comparison (that's why we used .lower())