DEV Community

eaarroyo
eaarroyo

Posted on

Introduction to Set Theory with python

A set is a collection of objects. For instance, let's say that 1, 2, 3 ,4, a ,b ,c , $ are objects. A set of those objects would be

S = {1, 2, 3, 4, a, b, c, $}

#python code
S = {1, 2, 3, 4, 'a', 'b', 'c', '$'}

#It is also possible to use the set() constructor to build a set
S = set((1, 2, 3, 4, 'a', 'b', 'c', '$'))

# Either way, you can make use of the methods:
S.update({'y', 'z'}) # Adds multiple elements to the set
S.add('w') # Adds one element to the set
S.remove('y') # Removes the element from the set
S.remove('z')
S.remove('w')
Enter fullscreen mode Exit fullscreen mode

To set are equal if they contain the same objects. Order and repetition does not matter.

{1, 2, 3, 4, a, b, c, $} is equal to {a, b, c, 1, 2, 3, 4, $, $, $}

#python code
V = {'a', 'b', 'c', 1, 2, 3, 4, '$', '$', '$'}
print(S == V) # Output is True

#If we print V we will notice that the output does not include repetitions

print(V) #output {1, 2, 3, 4, 'a', '$', 'c', 'b'}
Enter fullscreen mode Exit fullscreen mode

If, S = {1, 2, 3, 4, a, b, c, $}

a ∈ S, means object a is an element of the set S.

x ∉ S, means object s is not an element of the set S.

#python code
print('a' in S) #Output is True
print('x' in S) #Output is False
Enter fullscreen mode Exit fullscreen mode

The number of unique elements in a set is called Cardinality.

The cardinality of S is 8.

|S| = 8

#python code
print(len(S)) #Output is 8
Enter fullscreen mode Exit fullscreen mode

Set equivalence is when two or more sets have the same cardinality.

S is equivalent to V.

#python code
def areSetsEquivalent(setA, setB):
    if len(setA) == len(setB):
        return True
    else:
        return False

print(areSetsEquivalent(S, V)) # Output is true
Enter fullscreen mode Exit fullscreen mode

Operations

Cartisian Product

In Set Theory, there is an operation called Cartisian product.

It is denoted as:
S x V;

And read as:
S cross V.

It is the set of all ordered pairs (a, b) where a is in A and b is in B.

A x B = { (a,b) | a ∈ A and b ∈ B}

{a1, a2} x {b1, b2} = { (a1, b1), (a1, b2), (a2, b1), (a2, b2) }

Example

A = {1 , 2}
B = {3, 4}

A x B = { (1, 3), (1, 4), (2, 3), (2, 4) }

The cardinality of the Cartesian product is equal to the product of the Cardinality of the sets.

|A x B| = |A| * |B|;

|A x B| = 2 * 2;

|A x B| = 4;

#python code
from itertools import product
A = {1 , 2}
B = {3, 4}
print(set(product(A, B))) #Output {(2, 3), (2, 4), (1, 3), (1, 4)}
Enter fullscreen mode Exit fullscreen mode

Union

The union of a collection of sets is a set that contains all the elements of those sets.

A ∪ B = {x | x ∈ A or x ∈ B}

Example:

A = {1 , 2}
B = {3, 4}

A ∪ B = {1, 2, 3, 4}

#python code
A = {1 , 2}
B = {3, 4}

print(A.union(B)) # Output {1, 2, 3, 4}
Enter fullscreen mode Exit fullscreen mode

Intersection

The intersection of sets are all those objects that are common on all the sets.

A ∩ B = {x | x ∈ A and x ∈ B}

Example:

Y = {1, 2, 3}
Z = {3, 4, 5}

Y ∩ Z = {3}

#python code
Y = {1, 2, 3}
Z = {3, 4, 5}

print(Y.intersection(Z)) #Output {3}
Enter fullscreen mode Exit fullscreen mode

Conclusion

As you can see, python provide a lot of tools to work with sets. The topics exposed on the content above are just an introduction to the Set theory. There is a lot more related the topic, such as, Difference and complement, Disjoint sets, Naturally Disjoint sets, partition of sets, subsets and power sets. I would discuss all of those topics in different posts.

Written by: E. A. Arroyo.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more