<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: eaarroyo</title>
    <description>The latest articles on DEV Community by eaarroyo (@eaarroyo).</description>
    <link>https://dev.to/eaarroyo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2798774%2F28aebd03-fd41-4e98-82ab-ffe86ce01bfd.png</url>
      <title>DEV Community: eaarroyo</title>
      <link>https://dev.to/eaarroyo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eaarroyo"/>
    <language>en</language>
    <item>
      <title>Notes on set theory with Python. Part II.</title>
      <dc:creator>eaarroyo</dc:creator>
      <pubDate>Wed, 19 Mar 2025 01:01:50 +0000</pubDate>
      <link>https://dev.to/eaarroyo/introduction-to-set-theory-with-python-part-ii-3m54</link>
      <guid>https://dev.to/eaarroyo/introduction-to-set-theory-with-python-part-ii-3m54</guid>
      <description>&lt;p&gt;Topics: Set difference, complement of a set.&lt;/p&gt;

&lt;p&gt;On this post, we are talking about some other topics we didn´t covered on the previous one. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/eaarroyo/introduction-to-set-theory-with-python-3jol"&gt;https://dev.to/eaarroyo/introduction-to-set-theory-with-python-3jol&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's start with set difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set Difference
&lt;/h2&gt;

&lt;p&gt;The difference of two sets is writter as:&lt;/p&gt;

&lt;p&gt;A \ B or A - B.&lt;/p&gt;

&lt;p&gt;If A = {1, 2, 3} and B = {3, 4, 5}, the difference is a set of those objects of A that are not in B.&lt;/p&gt;

&lt;p&gt;e.g. A - B = {1, 2}&lt;/p&gt;

&lt;p&gt;In builder notation this would be:&lt;/p&gt;

&lt;p&gt;A\B = {x : x ∈ A and x ∉ B };&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Python Script
A = {1, 2, 3}
B = {3, 4, 5}

A.difference(B) #  Output is {1, 2}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Complement of a set
&lt;/h2&gt;

&lt;p&gt;The complement of a set are all those elements of the universal set that are not on the other set.&lt;/p&gt;

&lt;p&gt;So, for this, we need to define a Universal set such as:&lt;/p&gt;

&lt;p&gt;U = {1, 2, 3, 4}&lt;/p&gt;

&lt;p&gt;A = {2, 3}&lt;/p&gt;

&lt;p&gt;A' = {1, 4}&lt;/p&gt;

&lt;p&gt;A' is the complement of A. Being U the universal set.&lt;/p&gt;

&lt;p&gt;It can be written in builder notation like this:&lt;/p&gt;

&lt;p&gt;A' = {x ∈ U and x ∉ A };&lt;/p&gt;

&lt;p&gt;This present to us something really interesting:&lt;/p&gt;

&lt;p&gt;Being:&lt;/p&gt;

&lt;p&gt;U = {1, 2, 3, 4}&lt;/p&gt;

&lt;p&gt;A = {2, 3}&lt;/p&gt;

&lt;p&gt;U - A = {x : x ∈ U and x ∉ A };&lt;/p&gt;

&lt;p&gt;U - A = {1, 4}&lt;/p&gt;

&lt;p&gt;And,&lt;/p&gt;

&lt;p&gt;A' = {x ∈ U and x ∉ A };&lt;/p&gt;

&lt;p&gt;A' = {1, 4}&lt;/p&gt;

&lt;p&gt;So, A' = U - A;&lt;/p&gt;

&lt;p&gt;In python there is not a "Complement of a set" method, per se. So this would need to be done with the difference method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Python script
U = {1, 2, 3, 4}
A = {2, 3}

U.difference(A) # Output: {1, 4}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Written by E. A. Arroyo&lt;/p&gt;

</description>
      <category>programming</category>
      <category>mathematics</category>
      <category>python</category>
    </item>
    <item>
      <title>Notes on Set Theory with python. Part I</title>
      <dc:creator>eaarroyo</dc:creator>
      <pubDate>Fri, 31 Jan 2025 20:25:40 +0000</pubDate>
      <link>https://dev.to/eaarroyo/introduction-to-set-theory-with-python-3jol</link>
      <guid>https://dev.to/eaarroyo/introduction-to-set-theory-with-python-3jol</guid>
      <description>&lt;p&gt;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&lt;/p&gt;

&lt;p&gt;S = {1, 2, 3, 4, a, b, c, $}&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#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')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To set are equal if they contain the same objects. Order and repetition does not matter.&lt;/p&gt;

&lt;p&gt;{1, 2, 3, 4, a, b, c, $} is equal to {a, b, c, 1, 2, 3, 4, $, $, $}&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#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'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If, S = {1, 2, 3, 4, a, b, c, $}&lt;/p&gt;

&lt;p&gt;a ∈ S, means object &lt;strong&gt;a&lt;/strong&gt; is an element of the set S.&lt;/p&gt;

&lt;p&gt;x ∉ S, means object &lt;strong&gt;s&lt;/strong&gt; is not an element of the set S.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#python code
print('a' in S) #Output is True
print('x' in S) #Output is False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The number of unique elements in a set is called Cardinality.&lt;/p&gt;

&lt;p&gt;The cardinality of S is 8.&lt;/p&gt;

&lt;p&gt;|S| = 8&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#python code
print(len(S)) #Output is 8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set equivalence is when two or more sets have the same cardinality.&lt;/p&gt;

&lt;p&gt;S is equivalent to V.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#python code
def areSetsEquivalent(setA, setB):
    if len(setA) == len(setB):
        return True
    else:
        return False

print(areSetsEquivalent(S, V)) # Output is true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Operations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Cartisian Product
&lt;/h3&gt;

&lt;p&gt;In Set Theory, there is an operation called Cartisian product. &lt;/p&gt;

&lt;p&gt;It is denoted as:&lt;br&gt;
&lt;strong&gt;S x V;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And read as:&lt;br&gt;
&lt;strong&gt;S cross V.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is the set of all ordered pairs (a, b) where a is in A and b is in B.&lt;/p&gt;

&lt;p&gt;A x B = { (a,b) | a ∈ A and b ∈ B}&lt;/p&gt;

&lt;p&gt;{a1, a2} x {b1, b2} =  { (a1, b1), (a1, b2), (a2, b1), (a2, b2) }&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A = {1 , 2}&lt;br&gt;
B = {3, 4}&lt;/p&gt;

&lt;p&gt;A x B = { (1, 3), (1, 4), (2, 3), (2, 4) }&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The cardinality of the Cartesian product is equal to the product of the Cardinality of the sets.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;|A x B| = |A| * |B|;&lt;/p&gt;

&lt;p&gt;|A x B| = 2 * 2;&lt;/p&gt;

&lt;p&gt;|A x B| = 4;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#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)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Union
&lt;/h3&gt;

&lt;p&gt;The union of a collection of sets is a set that contains all the elements of those sets.&lt;/p&gt;

&lt;p&gt;A ∪ B = {x | x ∈ A or x ∈ B}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A = {1 , 2}&lt;br&gt;
B = {3, 4}&lt;/p&gt;

&lt;p&gt;A ∪ B = {1, 2, 3, 4}&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#python code
A = {1 , 2}
B = {3, 4}

print(A.union(B)) # Output {1, 2, 3, 4}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Intersection
&lt;/h3&gt;

&lt;p&gt;The intersection of sets are all those objects that are common on all the sets.&lt;/p&gt;

&lt;p&gt;A ∩ B = {x | x ∈ A and x ∈ B}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Y = {1, 2, 3}&lt;br&gt;
Z = {3, 4, 5}&lt;/p&gt;

&lt;p&gt;Y ∩ Z = {3}&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#python code
Y = {1, 2, 3}
Z = {3, 4, 5}

print(Y.intersection(Z)) #Output {3}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Written by: E. A. Arroyo.&lt;/p&gt;

</description>
      <category>python</category>
      <category>settheory</category>
      <category>discrete</category>
      <category>math</category>
    </item>
  </channel>
</rss>
