Learn

The complement of a set A - denoted A’ or Ac - is the set of all elements in the universal set U that are not in A. If the Universal set is W - the set of all whole numbers - and E is the set of all even numbers, then Ec represents all elements of W that are not members of E. That is,

W = {0, 1, 2, 3, 4, 5, …}

E = {0, 2, 4, …}

Ec = {1, 3, 5, …} = the set of all odd numbers

If our universal set is the set of all chess pieces, U = { king, knight, rook, bishop, queen, pawn} and if K = {king, knight}, then Kc = {rook, bishop, queen, pawn}.

You will find that a set and its complement together form their universal set!

The cardinality of a set is the number of elements in the set. For example, if A = {1, 3, 0, 7, 9}, then the cardinality of A is represented as |A| = 5. Going back to the chess pieces example, the number of elements in set U is 6, set K is 2, and set Kc is 4. This satisfies the relation

|U| = |K| + |Kc|

The len() function of Python can be used to find the cardinality of a set. The .difference() method returns a set that contains the items that only exist in one set and not the other. U.difference(K) will return the set Kc.

Instructions

1.

Write code in Python to create a set of the blue component colors in the rainbow, B = {blue, indigo, violet}.

2.

Use the difference method on sets R and B to find Bc.

Assign this set to a variable called Bc and then print the contents of Bc.

3.

Verify that |R| = |B| + |Bc|.

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?