First page Back Continue Last page Overview Graphics

Sets

A

A ∩ B

C

B

A ∩ B ∩ C

B ∩ C

A ∩ C

Sets are unordered collections of unique elements, which have to be “immutable” in Python.

Python 2:

A set can be created

iteratively:

>>> s = set()

>>> s.add("a")

>>> s.add("b")

>>> s.add("c")

>>> print(s)

set(['a', 'c', 'b'])

or directly while being initialized:

>>> s = set(["a","b","c"])

>>> print s

set(['a', 'c', 'b'])

Python 3:

A set can be created

iteratively:

>>> s = set()

>>> s.add("a")

>>> s.add("b")

>>> s.add("c")

>>> print(s)

{'b', 'c', 'a'}

or directly while being initialized:

>>> s = {"a","b","c"}

>>> print(s)

{'b', 'c', 'a'}