liveBook · Manning

Membership operator

As explained in Python Workout: 50 ten-minute exercises

Python's membership operator tests whether a value occurs within a container or iterable.

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

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains syntax, supported containers, dictionary key behavior, and linear search, but stops short of membership protocol internals.
Examples
Demonstrates vowel detection, dictionary-backed restaurant ordering, menu validation, prices, and running totals.
What it assumes
Assumes basic Python conditionals and containers while reinforcing that strings are iterable sequences.
What you can do after
Apply membership tests to search sequences, validate dictionary keys, and replace repetitive loops or comparisons.
How it connects
Connects membership tests with strings, sequences, lists, tuples, dictionaries, loops, and Pig Latin.

Also explained in

Compare how 8 books explain membership operator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.