Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 38 · Sets and Operations

Union and Intersection

Learn the union and intersection of sets, disjoint sets, the properties of both operations, and how to compute them by hand and in Python.

You can build new sets from old ones. The two most important operations are union (put everything together) and intersection (keep only what is shared). They mirror the logical words "or" and "and", and they appear everywhere: search filters, database queries, probability events, and the solution sets of inequalities.

Union

The union of A and B, written A ∪ B, is the set of elements that belong to A, or to B, or to both:

Text
A ∪ B = { x : x ∈ A or x ∈ B }

In mathematics "or" is inclusive: an element in both sets is in the union. Each element still appears only once.

Example. With A = {1, 2, 3} and B = {3, 4}, we get A ∪ B = {1, 2, 3, 4}.

Intersection

The intersection of A and B, written A ∩ B, is the set of elements that belong to both:

Text
A ∩ B = { x : x ∈ A and x ∈ B }

With the same sets, A ∩ B = {3}.

If two sets have no common element, A ∩ B = ∅, and we call them disjoint. For instance the even integers and the odd integers are disjoint.

flowchart LR
    A["A only: 1, 2"] --- I["both: 3"] --- B["B only: 4"]

A Venn diagram split into three regions: only in A, in both, only in B. The union is all three regions; the intersection is the middle one.

Counting the union

For finite sets, adding |A| and |B| counts the shared elements twice, so you subtract them once:

Text
|A ∪ B| = |A| + |B| − |A ∩ B|

In the example, 3 + 2 − 1 = 4. This is the simplest case of the inclusion-exclusion principle, which you will extend later in this chapter. If the sets are disjoint, the correction term is 0 and |A ∪ B| = |A| + |B|.

Python Union, intersection and the counting rule

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Properties

Both operations obey simple laws. For any sets A, B, C:

Law Union Intersection
Commutative A ∪ B = B ∪ A A ∩ B = B ∩ A
Associative (A ∪ B) ∪ C = A ∪ (B ∪ C) (A ∩ B) ∩ C = A ∩ (B ∩ C)
Idempotent A ∪ A = A A ∩ A = A
Identity A ∪ ∅ = A A ∩ U = A
Domination A ∪ U = U A ∩ ∅ = ∅
Distributive A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)

Because of associativity, you may write A ∪ B ∪ C without brackets, and likewise for intersections. The two distributive laws are a bit like multiplying out brackets in algebra, but note that both operations distribute over each other, which is not true for + and ×.

Two useful links with subsets:

  • A ⊆ A ∪ B and A ∩ B ⊆ A, always.
  • A ⊆ B is equivalent to A ∪ B = B, and equivalent to A ∩ B = A.

A proof of a distributive law

Prove A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) by double inclusion.

  • (⊆) Let x ∈ A ∩ (B ∪ C). Then x ∈ A, and x ∈ B or x ∈ C. If x ∈ B, then x ∈ A ∩ B; if x ∈ C, then x ∈ A ∩ C. Either way x ∈ (A ∩ B) ∪ (A ∩ C).
  • (⊇) Let x ∈ (A ∩ B) ∪ (A ∩ C). Then x ∈ A ∩ B or x ∈ A ∩ C. In both cases x ∈ A, and x is in B or in C, so x ∈ B ∪ C. Hence x ∈ A ∩ (B ∪ C).

Unions and intersections of many sets

You can take the union or intersection of a whole family A₁, A₂, ..., Aₙ, written ⋃ Aᵢ and ⋂ Aᵢ. The union holds everything that is in at least one Aᵢ; the intersection holds what is in every Aᵢ. A famous example uses intervals: for i = 1, 2, 3, ... let Aᵢ = (0, 1/i). Every Aᵢ is contained in (0, 1), the union of all of them is (0, 1), and the intersection is empty, because no positive number is smaller than 1/i for every i.

Python Families of sets

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Random testing like this gives confidence, but only a proof settles the matter for all sets.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Let A = {1, 2, 3, 4} and B = {3, 4, 5}. What is A ∩ B?

    The intersection contains the elements that belong to both sets: 3 and 4.
  2. 2. If |A| = 7, |B| = 5 and |A ∩ B| = 2, what is |A ∪ B|?

    7 + 5 - 2 = 10.
  3. 3. Which statement is equivalent to A ⊆ B?

    If A is inside B, then the shared part is all of A. Conversely, A ∩ B = A means every element of A is in B.
  4. 4. Which is true for all sets A and B?

    Anything in both sets is certainly in at least one of them.

Summary

  • A ∪ B is everything in A or B (inclusive or); A ∩ B is what is in both.
  • Sets with empty intersection are disjoint.
  • |A ∪ B| = |A| + |B| − |A ∩ B|.
  • Union and intersection are commutative, associative, idempotent and distribute over each other.
  • A ⊆ B is equivalent to A ∪ B = B and to A ∩ B = A.

Next: Complement and Difference