I was reviewing course material from week 1 to week 4 in order to prepare for my midterm exam.I found the majority of the content on the slides, like the quantifiers, proof structures and predicates
to be easy in nature. I kept on flipping the slides - page by page - until I stopped.
Something weird and alien had caught my eye. I read it again:
This was from week 4 lecture slides, it was in week 1 too!
I knew that code snippet 1 was the same as $\forall x \in S1, \exists y \in S2, x + y = 5$
and vice versa ... but I did not know why. I was thoroughly confused...
CSC108 did not teach me this! So in order to resolve my confusion for the approaching midterm, I had two options. 1) Blindly agree to the facts 2) conduct my own research into the subject (albeit at the cost of time).
I chose the latter, because I was curious. *Opens Google*
Quick research showed that I was looking at a concept called "set comprehensions". It can
be used to construct lists in a very natural and easy way, similar to list construction in mathematics.
In maths we write sets as:
The issue here is that the syntax is quite still quite cryptic, but the output of the mathematical set and
pythonic code will still be the same.
Lets see something simple: Here is code that squares a set of numbers:
Here comprehension iterates over the elements $e$ in range(10) and returns $e$ ** 2 ($e^2$) for every element.
Now that seems much simpler!
Here I explain some more examples from Week 1:
to be easy in nature. I kept on flipping the slides - page by page - until I stopped.
Something weird and alien had caught my eye. I read it again:
for all, one...one for allwhat is the difference between the two claims:
$\forall x \in S1, \exists y \in S2, x + y = 5$
$\exists y \in S2, \forall c \in S1, x + y = 5$
S1 = S2 = {1, 2, 3, 4} def forall_exists(S1, S2): # code snippet 1 return all(any({x + y == 5 for y in S2}) for x in S1) def exists_forall(S1, S2): # code snippet 2 return any(all({x + y == 5 for y in S2}) for x in S1)
if __name__ == '__main__':
print(forall_exists(S1, S2))
print(exists_forall(S1, S2))
This was from week 4 lecture slides, it was in week 1 too!
I knew that code snippet 1 was the same as $\forall x \in S1, \exists y \in S2, x + y = 5$
and vice versa ... but I did not know why. I was thoroughly confused...
![]() |
| An accurate portrayal of my reaction when I saw my first set comprehension |
CSC108 did not teach me this! So in order to resolve my confusion for the approaching midterm, I had two options. 1) Blindly agree to the facts 2) conduct my own research into the subject (albeit at the cost of time).
I chose the latter, because I was curious. *Opens Google*
![]() |
| Ohhhhhh. That was easier than I thought. |
Quick research showed that I was looking at a concept called "set comprehensions". It can
be used to construct lists in a very natural and easy way, similar to list construction in mathematics.
In maths we write sets as:
$S = \{x^2 : x \in \{0,1,2,3,4,5\}\}$
$V = \{1,2,4, \ldots, 2^{13}\}$
$M = \{x : x \in S \wedge x = 0 (mod 2) \}$
In python this is the same as:S = {x**2 for x in range(6)}
V = {2**i for i in range(14)}
M = {x for x in S if x % 2 == 0}
The issue here is that the syntax is quite still quite cryptic, but the output of the mathematical set and
pythonic code will still be the same.
Lets see something simple: Here is code that squares a set of numbers:
squares = set()
for x in range(10):
squares.add(x ** 2)
This will return the set: {0,1,4,9,16,25,36,49,64,81}
Well, we obtain the same result with the comprehension:square = {x ** 2 for x in range(10)}
Here comprehension iterates over the elements $e$ in range(10) and returns $e$ ** 2 ($e^2$) for every element.
Now that seems much simpler!
Here I explain some more examples from Week 1:
S2 = {x + 2 for x in S}- The comprehension iterates over every element x in S and makes a list S2 where every element is x + 2.
S3 = {x for x in S if x < 3}- We can even add predicates when making new sets!This comprehension iterates over every element x in S and adds x to S3 if x < 3 is True.
S4 = {x ** 2 for x in S if x > 3}- This is the combination of both 1) and 2). Here we add element $x^2$ to set S4 if element x satisfies the predicate x > 3!
I felt that the research into the topic has allowed me to understand set comprehension deeper than before. Now I am truly ready for the midterm!


No comments:
Post a Comment