Python Glossary Русский

all Python 3Built-in functionsterm

set

types

A mutable set of unique hashable items with no defined order. Supports union, intersection and difference operations.

Syntax

set(iterable)

An empty set can only be built with set(); empty curly braces give you a dict instead. Only hashable objects fit inside — a list raises TypeError, so convert it to a tuple or frozenset first. Iteration order is arbitrary and unrelated to insertion order, so print sorted(s) when the output has to be reproducible.

Examples

print(set([1,2,2,3]))  # → {1,2,3}
s1={1,2,3}; s2={2,3,4}
print(s1|s2)  # → {1,2,3,4} объединение
print(s1&s2)  # → {2,3} пересечение
print(s1-s2)  # → {1} разность
print(s1^s2)  # → {1,4} симм.разность

See also

Open in the full glossarydocs.python.orgReport an inaccuracy