REVISED: Sunday, March 3, 2013
This tutorial discusses Python 3 "Set Theory and Logic".
I. PYTHON 3 "SET THEORY AND LOGIC" INTRODUCTION
A set is an unordered collection of "distinct" objects; which means a set does not contain duplicate objects. Curly brackets { } are used when detailing the contents of a set. For example, the set A would be written as A = {a, b, c}. Set A has three members or elements: a, b, and c. The set A is no different from A = {b, a, c} because the ordering of the elements in a set does not matter. The only concept that really matters for any set is membership. The ∈ sign denotes set membership, and can be read as "in". If "a is a member of the set A" we write a ∈ A. And, if "d is not a member of set A", we could write d ¬∈ A.
In Python:
def main():
A = set( ['a', 'a', 'b', 'b','c', 'c' ] )
print('Set A =',A) print('"a" in A =',"a" in A)
print('"d" in A =',"d" in A)
if __name__ == '__main__':
main()
Python display:
def main():
# Python "Set Theory and Logic" Intersection
U = set(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'])
A = set( ['a', 'b', 'c'])
B = set( ['a', 'b', 'c', 'd'])
C = set(A).intersection(B)
print("Consider some universe U =",U)
print("If A and B are two sets taken from the universe U such that")
print("A =",A)
print("and")
print("B =",B)
print("then")
print("C = set(A).intersection(B) =",C)
if __name__ == '__main__':
main()
>>>
Consider some universe U = {'m', 'l', 'o', 'n', 'i', 'h', 'k', 'j', 'e', 'd', 'g', 'f', 'a', 'c', 'b', 'y', 'x', 'z', 'u', 't', 'w', 'v', 'q', 'p', 's', 'r'}
If A and B are two sets taken from the universe U such that
A = {'a', 'c', 'b'}
and
B = {'d', 'a', 'c', 'b'}
then
C = set(A).intersection(B) = {'a', 'c', 'b'}
>>>
def main():
# Python "Set Theory and Logic" Union
U = set(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'])
A = set( ['a', 'b', 'c'])
B = set( ['d', 'e', 'f'])
C = A.union(B)
print("Consider some universe U =",U)
print("If A and B are two sets taken from the universe U such that")
print("A =",A)
print("and")
print("B =",B)
print("then")
print("C = (A | B) =",C)
if __name__ == '__main__':
main()
>>>
Consider some universe U = {'k', 'j', 'i', 'h', 'o', 'n', 'm', 'l', 'c', 'b', 'a', 'g', 'f', 'e', 'd', 'z', 'y', 'x', 's', 'r', 'q', 'p', 'w', 'v', 'u', 't'}
If A and B are two sets taken from the universe U such that
A = {'c', 'b', 'a'}
and
B = {'f', 'e', 'd'}
then
C = (A | B) = {'c', 'b', 'a', 'f', 'e', 'd'}
>>>
D. DIFFERENCE
In English: Given two sets: A = set( ['a', 'a', 'b', 'b', 'c', 'c', 'd','e', 'f' ] ) and B = set( ['a', 'b', 'c' ] ), the difference, C = A - B, will be such that an element will be in set C if it is in set A and not in set B.
In Python:
>>>
Set A = {'a', 'b', 'c'}
Set A = {'a', 'b', 'c'}
"a" in A = True
"d" in A = False
>>>
A. CONDITIONAL STATEMENTS
In English: In mathematics the study of logic deals with statements or propositions. A statement is "a sentence that is either true or false, but not both". A conditional statement is two simple statements connected by the words if. . .then. (Dots like this “. . .” mean continue the pattern in the obvious way.) The first statement is called the hypothesis and the second statement is called the conclusion. Conditional statements are used in proving the validity of an argument through deductive reasoning. For example, if "it is 32 degrees Celsius or colder outside" then "the water outside is frozen".
In Python:
def main():
Celsius = 32
if Celsius >= 32:
print("The water outside is frozen.")
if __name__ == '__main__':
main()
>>>
The water outside is frozen.
>>>
def main():
Celsius = 32
if Celsius >= 32:
print("The water outside is frozen.")
if __name__ == '__main__':
main()
Python display:
>>>
The water outside is frozen.
>>>
B. INTERSECTION
In English: If A and B are two sets taken from some universe U, then the intersection of A and B written A ∩ B, or A ∧ B, is the set containing all elements common to sets A and B. An element is in the result if it is in one set and the other.
In Python:
def main():
# Python "Set Theory and Logic" Intersection
U = set(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'])
A = set( ['a', 'b', 'c'])
B = set( ['a', 'b', 'c', 'd'])
C = set(A).intersection(B)
print("Consider some universe U =",U)
print("If A and B are two sets taken from the universe U such that")
print("A =",A)
print("and")
print("B =",B)
print("then")
print("C = set(A).intersection(B) =",C)
if __name__ == '__main__':
main()
Python display:
>>>
Consider some universe U = {'m', 'l', 'o', 'n', 'i', 'h', 'k', 'j', 'e', 'd', 'g', 'f', 'a', 'c', 'b', 'y', 'x', 'z', 'u', 't', 'w', 'v', 'q', 'p', 's', 'r'}
If A and B are two sets taken from the universe U such that
A = {'a', 'c', 'b'}
and
B = {'d', 'a', 'c', 'b'}
then
C = set(A).intersection(B) = {'a', 'c', 'b'}
>>>
C. UNION
In English: If A and B are two sets taken from some universe U, then the union of A and B written A U B, or A ∨ B, is the set containing all elements in either A or B. An element is in the result if it is one set or the other.
In Python:
def main():
# Python "Set Theory and Logic" Union
U = set(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'])
A = set( ['a', 'b', 'c'])
B = set( ['d', 'e', 'f'])
C = A.union(B)
print("Consider some universe U =",U)
print("If A and B are two sets taken from the universe U such that")
print("A =",A)
print("and")
print("B =",B)
print("then")
print("C = (A | B) =",C)
if __name__ == '__main__':
main()
Python display:
>>>
Consider some universe U = {'k', 'j', 'i', 'h', 'o', 'n', 'm', 'l', 'c', 'b', 'a', 'g', 'f', 'e', 'd', 'z', 'y', 'x', 's', 'r', 'q', 'p', 'w', 'v', 'u', 't'}
If A and B are two sets taken from the universe U such that
A = {'c', 'b', 'a'}
and
B = {'f', 'e', 'd'}
then
C = (A | B) = {'c', 'b', 'a', 'f', 'e', 'd'}
>>>
D. DIFFERENCE
In English: Given two sets: A = set( ['a', 'a', 'b', 'b', 'c', 'c', 'd','e', 'f' ] ) and B = set( ['a', 'b', 'c' ] ), the difference, C = A - B, will be such that an element will be in set C if it is in set A and not in set B.
In Python:
def main():
A = set( ['a', 'a', 'b', 'b', 'c', 'c', 'd','e', 'f' ] )
B = set( ['a', 'b', 'c' ] )
C = A - B
print("C =",C)
if __name__ == '__main__':
main()
Python display:
>>>
C = {'f', 'd', 'e'}
>>>
II. PYTHON 3 SET OPERATIONS
There are a large number of set operations, including "Union" (|), "Intersection" (&), "Difference" (-), and "Symmetric Difference" (^), also called "Exclusive Or", which is all elements in set A or set B but not both. You can also test to see if A is a Subset of B and if B is a Superset of A.
In Python:
def main():
A = set( ['a', 'a', 'b', 'b', 'c', 'c', 'd','e', 'f' ] )
B = set( ['a', 'b', 'c', 'z' ] )
C = set( ['a', 'b', 'c', 'w', 'z' ] )
print('The Union A | B is a new set with elements from both A and B =', A | B, "\n")
print('The Intersection A & B is a new set with elements common to A and B =', A & B, "\n")
print('The Difference A - B is a new set with elements in A but not in B =', A - B, "\n")
print('The Symmetric Difference A ^ B is a new set with elements in either A or B but not both =', A ^ B, "\n")
print('Is B a subset of C? In other words, is every element in B also in C?',B <= C, "\n")
print('Is C a superset of B? In other words, is every element in B also in C?',C >= B)
main()
Python display:
>>>
The Union A | B is a new set with elements from both A and B = {'z', 'd', 'e', 'f', 'a', 'b', 'c'}
The Intersection A & B is a new set with elements common to A and B = {'a', 'b', 'c'}
The Difference A - B is a new set with elements in A but not in B = {'d', 'e', 'f'}
The Symmetric Difference A ^ B is a new set with elements in either A or B but not both = {'z', 'd', 'e', 'f'}
Is B a subset of C? In other words, is every element in B also in C? True
Is C a superset of B? In other words, is every element in B also in C? True
>>>
III. REFERENCES
The Python Tutorial by Guido van Rossum and Fred L. Drake, Jr., editor (2003)
Style Guide for Python Code by Guido van Rossum and Barry Warsaw (2001)
In this tutorial, you have been presented with a basic introduction to Python 3 "Set Theory and Logic".
Elcric Otto Circle
-->
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to: ELCRIC OTTO CIRCLE's Home Page"
Set theory can be used to compare and contrast. Using a Venn Diagram Maker we can create venn diagrams of 2-sets, 3-sets/4-sets or get creative with the app Creately. There are 100s of diagram templates and examples to be used freely in the diagram community of Creately online diagramming and collaboration software.
ReplyDelete