Sets & Tuples

Yamac Eren Ay
4 min readMay 7, 2022

--

Source: https://realpython.com/python-lists-tuples/

Sets and tuples are the basic list structures in the math language.

In the following, we will see very important features of sets and tuples along with the operations defined on sets and tuples.

Let’s start with the features.

Features of Set

Source: https://realpython.com/python-sets/

Set is a list of values.

{1, 2, 3} is a set.

{1, 1, 3} is equal to {1, 3} because duplicates are not allowed.

{1, 2, 3} is equal to {1, 3, 2} because the sequence order of elements is not stored.

1 ∈ {1, 2, 3} but 4 ∉ {1, 2, 3}.

Subset notation: {1, 2} ⊆{1, 2, 3} but {1, 4} ⊈ {1, 2, 3}.

2^A = {B | B ⊆ A} is called a power set and contains every subset of A.

B ∈ 2^A, if and only if B ⊆ A.

The function “||” outputs the number of elements in a set: |{1, 2, 3}| = 3.

{} is the empty set and the only set which “||” maps to 0.

{1, 2, …, n} has the start index 1, the step pattern 2–1 = 1, and the end index n.

Special cases: {} if n < 1, {1} if n = 1.

{1, …, 10} includes all numbers from 1 to 10.

We can also represent it as {i is a (natural) number | i ≥ 1, i ≤ n} = {i is a (natural) number | 1 ≤ i ≤ 10}.

Notice that “,” on the right side is equal to logical AND.

ℕ is the set of all natural numbers starting from 0, 1, … and can be represented as {0, 1, …}, since there is no upper limit.

is the set of all real numbers.

The real numbers between a and b (inclusively) are defined as [a, b].

If a is excluded from the interval: (a, b] and vice versa.

Features of Tuple

Source: https://www.dotnetperls.com/tuple-fs

Tuple is an ordered list of values.

Unlike sets, it can hold sequence order.

(1, 2, 3) is a tuple.

Duplicate values are allowed, for example: (1, 2, 2, 3) is not equal to (1, 2, 3).

(1, 2, 3) is not equal to (1, 3, 2).

(1, 2, …, m) defines the range of numbers as a tuple array.

As a shorthand notation, we can define (i)_{i ∈ {1, …, n}} or (i)_{1 ≤ i ≤ n}, it is similar to {i | 1 ≤ i ≤ n} in sets.

Operations

Metaphorically, sets are the arrays and tuples are the structs in C.

In an array, the content of the array and overall quantity is much of importance, while in a struct, the order and semantics of values play a significant role. For example:

  • 5 ∈ (3, 5, 10) doesn’t make sense, but 5 = (3, 5, 10)_2 makes sense, because a tuple is a struct and each item in a tuple stores a different ‘column’ feature.
  • If we really would want to check if 5 ∈ (3, 5, 10), then it should have been defined as a set, since the order is not relevant.

So there is a clear distinction between sets and tuples, and in which context they are used.

The operations of sets and tuples are shown below:

Set Operations

Union: A ∪ B means: {x | “x is in A or in B”} = {x | x ∈ A ∨ x ∈ B}. Assuming A_1, …, A_n are all sets, ⋃_i A_i defines the union of all sets.

Intersection: A ∩ B means: {x | “x is in A and in B”} = {x | x ∈ A ∧ x ∈ B}. Assuming A_1, …, A_n are all sets, ∩_i A_i defines the intersection of all sets.

Difference: A \ B means: {x | x is in A but not in B} = {x | x ∈ A ∧ x ∉ B}.

Product: A × B means: {(a_i, b_j) | a_i ∈ A, b_j ∈ B}. Assuming A_1, …, A_n are all sets, ∏_i A_i defines the (cross) product of all sets. If all sets A_i are identical, ∏_{1 ≤ i ≤ n} A equals to A^n.

Tuple Operations

Indexing: Assuming w = (w_1, …, w_n) is a tuple. w_i selects the i-th element from the tuple w.

--

--

Yamac Eren Ay
Yamac Eren Ay

No responses yet