Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 38 ยท Getting Started

What Is a Set?

Learn what a set is, how to write sets with braces and set-builder notation, and how to test membership, with runnable Python examples.

A set is a collection of distinct objects, considered as a single thing. The objects can be anything: numbers, letters, words, people, even other sets. Set theory is the part of math that studies sets, and almost all of modern mathematics is written in its language. Functions, numbers, geometry and probability can all be built from sets.

In this lesson you will learn how to write a set, how to say that something belongs to it, and the two main ways to describe a set. You will also see how Python's built-in set type behaves like the mathematical idea, and where it differs.

The core idea: membership

A set is completely determined by which objects belong to it. We call those objects the elements (or members) of the set. We write x โˆˆ A to say "x is an element of A", and x โˆ‰ A to say "x is not an element of A".

Two rules make sets different from lists:

  • No repeats. Writing an object twice does not make it count twice. The set {1, 2, 2, 3} is the same set as {1, 2, 3}.
  • No order. {1, 2, 3} and {3, 1, 2} are the same set. Only membership matters.

An object is either in a set or it is not. There is no "twice in" and no "first in". If you need repeats or order, you want a list, a tuple or a multiset, which are different structures.

Roster notation

The simplest way to describe a finite set is to list its elements between curly braces. This is called roster notation (or listing notation).

Text
A = {2, 4, 6, 8}
V = {a, e, i, o, u}

Here 4 โˆˆ A is true, and 5 โˆ‰ A is true. For a long but patterned set you may use dots: {1, 2, 3, ..., 100} means the whole numbers from 1 to 100. Use the dots only when the pattern is obvious.

Set-builder notation

Often a set is better described by a rule than by a list. Set-builder notation has the form

Text
{ x : x has property P }

and is read "the set of all x such that x has property P". Some books use a vertical bar, { x | P(x) }, instead of the colon. You can also say where x comes from:

Text
E = { n โˆˆ โ„ค : n is even }
S = { nยฒ : n โˆˆ {1, 2, 3, 4} }

The first set is all even integers. The second is {1, 4, 9, 16}: you square each element of {1, 2, 3, 4}.

Python's set comprehension is almost a direct translation of this notation. Compare { nยฒ : n โˆˆ {1,2,3,4} } with {n**2 for n in {1, 2, 3, 4}}.

Python Membership and duplicates

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.

Sets can contain sets

An element of a set may itself be a set. Consider B = {1, {2, 3}}. It has exactly two elements: the number 1 and the set {2, 3}. The numbers 2 and 3 are not elements of B. They are elements of an element of B. This is the most common source of confusion for beginners, so keep it in mind:

  • 1 โˆˆ B is true.
  • {2, 3} โˆˆ B is true.
  • 2 โˆˆ B is false.

In Python, a set cannot contain another ordinary set, because Python only allows hashable (unchangeable) objects inside a set. The unchangeable version of a set is frozenset.

Python A set 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.

Cardinality: how many elements?

For a finite set A, the number of elements is written |A| and called the cardinality (or size) of A. For A = {2, 4, 6, 8} we have |A| = 4. Later in this track you will extend the idea of "size" to infinite sets, and some surprising things happen. For now, remember that |A| counts distinct elements only.

The empty set

The set with no elements is the empty set, written โˆ… or {}. It is a perfectly good set. Careful: โˆ… and {โˆ…} are different. The first has zero elements; the second has exactly one element, namely the empty set. You will meet this again in the lesson on power sets.

Well-defined sets

A set must be well-defined: for any object, there must be a clear yes or no answer to "is it in the set?". "The set of tall students" is not well-defined because "tall" is vague. "The set of students taller than 180 cm" is well-defined. At the end of the track you will see that even apparently precise rules can cause trouble when they are too free, and how the axioms of set theory protect us.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many elements does the set {1, 2, 2, 3, 3, 3} have?

    Repeats do not count. The distinct elements are 1, 2 and 3, so the set has 3 elements.
  2. 2. Let B = {1, {2, 3}}. Which statement is true?

    B has two elements: the number 1 and the set {2, 3}. The numbers 2 and 3 are not elements of B itself.
  3. 3. What does { nยฒ : n โˆˆ {1, 2, 3} } equal?

    Square each element: 1ยฒ = 1, 2ยฒ = 4, 3ยฒ = 9.
  4. 4. Which statement about {1, 2, 3} and {3, 2, 1} is correct?

    A set is determined only by its elements, not by the order in which they are written.

Summary

  • A set is a collection of distinct objects; the objects are its elements.
  • x โˆˆ A means x is an element of A; x โˆ‰ A means it is not.
  • Order and repeats do not matter.
  • Describe sets by listing (roster notation) or by a rule (set-builder notation).
  • Elements can be sets themselves; be careful to tell 2 โˆˆ B apart from โІ B.
  • |A| is the number of elements of a finite set; the empty set โˆ… has size 0.

Next: Equality and Subsets