Learn

Let us start with the simplest examples of sets.

  • The empty set (or the null set), is what it sounds like, the set with no elements. We usually denote it by ∅ or by { }.

  • The number of elements in a set may be infinite. For example, N is the set of all natural numbers. We do not need to list all the elements. This set can be represented as N = {1, 2, 3, …}.

  • A set can also be bidirectional. For example, Z, the set of all integers = {…, -2, -1, 0, 1, 2, …}.

  • A universal set is a set that contains all the elements of other sets (and its own elements). It is usually denoted by the symbol ‘U’. Suppose set E is the set of all even numbers, or E = {0, 2, 4, 6, …}, and set O is the set of all odd numbers, or O = {1, 3, 5, …}. The set of whole numbers, W, is a Universal set that contains both sets E and O.

  • A subset is a set formed by taking parts of the original set. For example, the set of even numbers, E, is a subset of the set of whole numbers, W. This is represented as E ⊂ W. However the set {-1, 0, 1} is not a subset of W, so {-1, 0, 1} ⊄ W.

Let us look at another example - the collection of possible outcomes of an experiment, like a die roll. D is the set of all outcomes when rolling a die, or D = {1, 2, 3, 4, 5, 6}.

These are some subsets of D: {1}, {}, {5}, {1, 3, 5}, {2, 4}.

In Python, you can loop through the set items using a for loop. The .issubset() method can be used to check if one set is a subset of another. To create an empty set in Python, use set(), not {}. The latter creates an empty dictionary.

Instructions

1.

Four sets have been created in the workspace. Use a for loop to display all the elements of the set R.

2.

Check if set P is a subset of R, and print your result.

3.

One by one, check if B and E are subsets of R, and print your result.

Take this course for free

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