Meaning
Returns the intersection of the set stored in variable `fruits` with the set literal {'apple', 'banana'}, yielding a new set containing only the elements present in both operands.
Primary Function
Compute set intersection between a variable set and a literal set.
Communicative Purpose
Express the operation of finding common elements between a variable set of fruits and a specific set of target fruits.
Pattern
<set_var> & {<element1>, <element2>, ...}
Core Structure
<set_variable> & {<element1>, <element2>, ...}
Função primária
Compute set intersection between a variable set and a literal set.
Propósito comunicativo
Express the operation of finding common elements between a variable set of fruits and a specific set of target fruits.
Situações de gatilho
When filtering a collection of fruits to see which of a specific subset are present, or when needing to keep only certain allowed values from a set.
Contextos
Data processing, filtering collections, set-based logic in Python scripts, algorithmic filtering, validation of allowed categories.
Padrão
<set_var> & {<element1>, <element2>, ...}
Estrutura central
<set_variable> & {<element1>, <element2>, ...}
Slots de substituição
<set_var>: any set variable (or frozenset); {<element1>, <element2>, ...}: a set literal of elements to intersect with.
Colocados típicos
- set
- intersection
- &
- fruits
- apple
- banana
- my_set
- allowed_values
Substituições comuns
- Any set variable can replace `fruits`
- any set literal can replace {'apple'
- 'banana'} (e.g.
- {'apple'}
- {'banana'
- 'cherry'}).
Erros comuns
Using '&' on non-set types (e.g., lists) causing TypeError; forgetting curly braces for set literal and using '&' with strings; confusing '&' with logical 'and' on booleans.
Similar / contraste
Union operator '|' (union), difference operator '-', symmetric difference operator '^'; method equivalents .intersection(), .difference(), .symmetric_difference().
Interferências
Coming from C/C++: using '&' as bitwise AND on integers, expecting set intersection → results in bitwise operation, not set intersection; ensure operands are sets or frozensets. Coming from JavaScript: using '&' as bitwise AND, expecting logical AND → unexpected results; use '&' only for set intersection with set/frozenset.
Família do chunk
- set_operations
Nuance
Do not use '&' for non-set types (e.g., lists, booleans) as it causes TypeError or unintended bitwise results; performance-wise, '&' creates a new set, leaving originals unchanged, which is efficient for small sets but may allocate memory for large sets; note that the operator is commutative and associative, but order of evaluation does not affect result, though intermediate temporaries may affect memory usage.
Efeito pragmático
Communicates an intent to filter or restrict a set to only those elements that match a specific criterion.
Dica de memória
Think of the ampersand as ‘and’ for sets: what’s in both groups?
Nota
The '&' operator works only with set and frozenset objects in Python; for other sequences, convert to set first or use the .intersection() method.
Upgrade path
Learn other set operators (|, -, ^) and their method counterparts (.union, .difference, .symmetric_difference) and explore set comprehensions for more complex filtering.
Log in to save chunks.