ENع
←Sets
Lesson 07 · Logic and sets

Sets

12 min10 steps5 questionsBeginner
About this lesson

A set is a collection of different things, with no order and no repeats. Follow two small sets, A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7}, on a Venn diagram. Watch elements land in their zones, then see union, intersection, difference and symmetric difference collect their answers, count a union without double counting, and meet subsets, the universe and the complement, each next to the Python that does it.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 7 · Interactive
01 / 10

A set of elements

A set is a collection of different things, called its elements. We write a set in curly braces: A = {1, 2, 3, 4, 5}. Order does not matter, and each element is simply in the set or not.

>>> A = {1, 2, 3, 4, 5}
>>> A
{1, 2, 3, 4, 5}
>>> {5, 4, 3, 2, 1} == A      # order does not matter
True
>>> {1, 1, 2} == {1, 2}        # nor does repeating
True
Loading figure…
or ← →
Read the lesson step by step
  1. A set of elements

    A set is a collection of different things, called its elements. We write a set in curly braces: A = {1, 2, 3, 4, 5}. Order does not matter, and each element is simply in the set or not.

    >>> A = {1, 2, 3, 4, 5}
    >>> A
    {1, 2, 3, 4, 5}
    >>> {5, 4, 3, 2, 1} == A      # order does not matter
    True
    >>> {1, 1, 2} == {1, 2}        # nor does repeating
    True
  2. Is it in the set?

    The symbol ∈ means "is an element of". 3 ∈ A is true, because 3 sits inside the box of A. 6 ∉ A, because 6 is not in A. Python asks the same question with the keyword in.

    >>> A = {1, 2, 3, 4, 5}
    >>> 3 in A
    True
    >>> 6 in A
    False
    >>> 6 not in A
    True
  3. A second set, B

    Now B = {4, 5, 6, 7}. 4 and 5 are already in A, so the box of B is drawn around them too. Nothing is copied: each element appears once. Overlapping boxes like these are a Venn diagram.

    >>> B = {4, 5, 6, 7}
    >>> set([4, 5, 5, 6, 7, 7])      # duplicates vanish
    {4, 5, 6, 7}
    >>> len(B)
    4
  4. Union: A ∪ B

    The union A ∪ B holds every element that is in A, in B, or in both. Python writes it A | B. Each element is listed once, so A ∪ B = {1, 2, 3, 4, 5, 6, 7}.

    >>> A = {1, 2, 3, 4, 5}
    >>> B = {4, 5, 6, 7}
    >>> A | B
    {1, 2, 3, 4, 5, 6, 7}
    >>> A.union(B)
    {1, 2, 3, 4, 5, 6, 7}
  5. Intersection: A ∩ B

    The intersection A ∩ B keeps only the elements that are in both sets: the overlap of the two boxes. Python writes it A & B. Here A ∩ B = {4, 5}.

    >>> A = {1, 2, 3, 4, 5}
    >>> B = {4, 5, 6, 7}
    >>> A & B
    {4, 5}
    >>> A.intersection(B)
    {4, 5}
  6. Difference: A − B

    The difference A − B keeps what is in A but not in B: take the part of B out of the box of A. So A − B = {1, 2, 3}. Order matters here: B − A = {6, 7}.

    >>> A = {1, 2, 3, 4, 5}
    >>> B = {4, 5, 6, 7}
    >>> A - B
    {1, 2, 3}
    >>> B - A
    {6, 7}
  7. Symmetric difference

    The symmetric difference A △ B keeps what is in exactly one of the sets: in A or in B, but not in both. Python writes it A ^ B. It is the union minus the overlap: {1, 2, 3, 6, 7}.

    >>> A = {1, 2, 3, 4, 5}
    >>> B = {4, 5, 6, 7}
    >>> A ^ B
    {1, 2, 3, 6, 7}
    >>> (A | B) - (A & B)
    {1, 2, 3, 6, 7}
  8. Counting with the overlap

    The size of a set, written |A|, is how many elements it has: |A| = 5 and |B| = 4. Adding them counts 4 and 5 twice, so take the overlap away once: |A ∪ B| = 5 + 4 − 2 = 7.

    >>> A = {1, 2, 3, 4, 5}
    >>> B = {4, 5, 6, 7}
    >>> len(A) + len(B) - len(A & B)
    7
    >>> len(A | B)
    7
  9. Subsets: C ⊆ A

    C is a subset of A, written C ⊆ A, when every element of C is also in A. Take C = {4, 5}: both are in A, so C ⊆ A is true. In Python, C <= A gives True.

    >>> A = {1, 2, 3, 4, 5}
    >>> C = {4, 5}
    >>> C <= A
    True
    >>> C.issubset(A)
    True
    >>> {4, 6} <= A          # 6 is not in A
    False
  10. The universe and complement

    The universe U holds every element we are talking about; here U = {1, 2, ..., 8}. The complement of A is everything in U that is not in A: U − A = {6, 7, 8}.

    >>> A = {1, 2, 3, 4, 5}
    >>> U = set(range(1, 9))
    >>> sorted(U - A)        # sets print in no set order
    [6, 7, 8]
    >>> len(U - A) == len(U) - len(A)
    True

Quiz Unlocks after the last step.