04 Sets
1. Overview
- Set: is an unordered collection of unique objects.
- A set can be created from a tuple or list
Shell
>>> a = [3, 4, 5]
>>> s = set(a)
>>> print(s)
{3, 4, 5}
>>> type(s)
<class 'set'>2. Some notes
2.1 Duplicates
Important
Duplicates are ignored
Shell
>>> a = [3, 4, 5]
>>> s = set(a)
>>> print(s)
{3, 4, 5}
>>> type(s)
<class 'set'>
>>>
>>> a = [3, 3, 5]
>>> s = set(a)
>>> print(s)
{3, 5}
>>> 2.2 The | operator and the & operator
Shell
>>> a = set([3, 4, 5])
>>> b = set([4, 5, 6])
>>> print(a)
{3, 4, 5}
>>> print(b)
{4, 5, 6}
>>> print(a | b)
{3, 4, 5, 6}
>>> print(a & b)
{4, 5}
>>> 2.3 The - operator and the ^ operator
Shell
>>> print(a)
{3, 4, 5}
>>> print(b)
{4, 5, 6}
>>> print(b - a)
{6}
>>> print(b ^ a)
{3, 6}
>>>