Python’s membership operator, in, tests whether a value occurs in a sequence or collection. It replaces repetitive loops and long chains of comparisons with a readable Boolean expression. section 2.3 section 2.0
Testing a vowel
Instead of comparing the first character with every vowel, write: if word[0] in 'aeiou':. This checks membership in the string and supports a compact Pig Latin decision. Searching a sequence takes linear time, which is often suitable for short strings. section 2.3
if word[0] in 'aeiou':
Compare the membership expression with loop-based and repetitive alternatives when choosing a clearer implementation. section 2.3
Apply in across common Python containers:
- Search strings, lists, tuples, and many other collections for an element. section 2.3
- Search a dictionary’s keys, not its values. section 2.3
- Validate restaurant orders before retrieving prices and updating a running total. section 4.3 section 4.4







