A bibliophile could own a collection of books, and a music lover may own a collection of CDs. Any collection of unique items can form a set. The objects that make up a set could be anything: movies, students, numbers, or even other sets. You can never tell when set notation will show up - it can be in your math class, philosophy class, or data science class. Knowledge of the symbols used in set theory is an asset!

Sets are conventionally denoted by capital letters. They can be defined by describing the contents, or by listing the elements of the set in curly brackets. Here are some examples of sets:

  • A = {1, 2, 3}
  • B is the set of all books written by Eiichiro Oda
  • P is the set given by the rule “prime numbers less than 15” or P = {2, 3, 5, 7, 11, 13}
  • G = {c, o, d, e, a, m, y}, the set of letters in the word ‘codecademy’

A set is unordered and unindexed. Unordered means that the elements of a set are stored in a random order. Unindexed means that we cannot access the elements of a set using indexes, unlike arrays and lists.

An object in a set is called an element (or member) of the set. The symbol ∈ is used to denote that an element is a member of a set and ∉ is used to denote that an object is not a member of a set. For example, if set A = {1, 3, 5} then 1 ∈ A, but 7 ∉ A.

Set is one of the four built-in data types in Python. The Python set is both unordered and unindexed. Here is the code to create a set -

Fruits = {"apple", "blueberry", "peach", "cherry"}

You cannot access items in a set by referring to an index or a key, but you can determine if a specified item is present in a set using the in keyword.

if "cherry" in Fruits: print("Yes, 'cherry' is in the Fruits set")



Write Python code to create the set R, the set of all colors of the rainbow.


Check if "pink" is an element 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?