In this section, we will touch on the story of set theory and operations. The content will be mainly covered in high school mathematics, and I think there will be a lot of theoretical talk.
Therefore, this section is [Supplement] </ font>, so you can skip it.
However, if you are taking the Fundamental Information Technology Engineer Examination or are thinking about choosing Python for the afternoon questions, you will be asked a set story in the field of basic theory, so please take a look.
First, to explain set theory, let's enter the following code into the ** Python console ** to create a set.
>>> A = {2, 4, 5, 6}
>>> B = {1, 2, 3, 4, 7}
>>> A
{2, 4, 5, 6}
>>> B
{1, 2, 3, 4, 7}
I created two sets this time, but the figures are as follows. This diagram is called a ** Venn diagram **.
As you can see from this Venn diagram, ** {2, 4} ** are common to each set in ** A ** and ** B **, respectively. Therefore, the Venn diagram can be expressed as follows.
Since there are many theoretical stories here, I will explain using the general expression "set" instead of the expression "set".
A ** set ** is a set that is gathered under certain ** conditions **. In the above example, a set of numerical values was created, but for example, set X "people with hair length less than 1 cm" and set Y "people with height 170 cm or more" are ** conditions **.
For example, "a person with short hair" and "a person with high height" depends on the subjectivity of the person, so this is not a condition. Specifically, think of the condition as being expressed numerically.
Here, we will introduce the operations in sets.
It is the sum of two sets. In the example of the set ** A ** and the set ** B ** above, the ones contained in either one or more sets ** are output. (Sometimes called ** OR operation **.) It is expressed by one of the following notations.
A∪B, A+B, A∨B, A or B
Therefore, it can be expressed as follows.
A∪B = \{1, 2, 3, 4, 5, 6, 7\}
In the Venn diagram, the following colors are applied.
When implemented in Python, it will be as follows. (Use the ** | ** (bar) at the "\ symbol" on the keyboard.)
>>> A | B
{1, 2, 3, 4, 5, 6, 7}
Alternatively, you can also use the ** union method **.
>>> A.union(B)
{1, 2, 3, 4, 5, 6, 7}
It is the product of two sets. In the example of the set ** A ** and the set ** B ** above, the ones contained in both ** sets ** are output. (Sometimes called ** AND operation **.) It is expressed by one of the following notations.
A∩B,A / B, A∧B, A and B
Therefore, it can be expressed as follows.
A∩B = \{2, 4\}
In the Venn diagram, the following colors are applied.
When implemented in Python, it will be as follows.
>>> A & B
{2, 4}
Alternatively, you can also use the ** intersection method **.
>>> A.intersection(B)
{2, 4}
It is one set minus the elements of another set. In the example of the set ** A ** and the set ** B ** above, the ** items that are included in the set A but not in the set B are output. It is expressed by the following notation method.
A-B
Therefore, it can be expressed as follows.
A-B = \{5, 6\}
In the Venn diagram, the following colors are applied.
When implemented in Python, it will be as follows.
>>> A - B
{5, 6}
Alternatively, it can be calculated using the ** difference method **.
>>> A.difference(B)
{5, 6}
It's a bit difficult to express, but the output is the one that meets either condition minus the one that meets both conditions (sometimes called ** XOR operation **). What this means is that A-B and B-A are performed on the difference set, and each is ORed. It is expressed by the following notation method.
A⊕B, A xor B
Therefore, it can be expressed as follows.
A⊕B = \{1, 3, 5, 6, 7\}
In the Venn diagram, the following colors are applied.
When implemented in Python, it will be as follows.
>>> A ^ B
{1, 3, 5, 6, 7}
Alternatively, it can be calculated using the ** symmetric_difference method **.
>>> A.symmetric_difference(B)
{1, 3, 5, 6, 7}
It means the negation of a set. It means that it is not included in the set ** A **. (Sometimes called ** NOT operation **.) Note that the complement cannot be implemented in Python. It is expressed by the following notation method.
\overline{A}, not A
Therefore, it can be expressed as follows.
\overline{A} = {1, 3, 7}
In the Venn diagram, the following colors are applied. * </ font> The complement covers the part other than the set ** A **. Therefore, the set ** B ** is also a range, and the outer part is also a range.
A ** subset ** is a set that contains more sets. The Venn diagram is as follows. Here, it is represented by the set ** P ** and the set ** Q **.
First, let's create a set from ** Python Console **.
>>> P = {1, 2, 3, 4, 5, 6, 7}
>>> Q = {3, 5}
>>> P
{1, 2, 3, 4, 5, 6, 7}
>>> Q
{3, 5}
In this state, the intersection is expressed as follows.
>>> P & Q
{3, 5}
The overlapping part of the set ** P ** and the set ** Q ** is displayed. You can see that this is the same as the result of the intersection set described earlier.
So how do you express that the set ** Q ** is contained in the set ** P **? It is expressed by the following notation method.
Q⊂P, Q⊆P
In other words, it means that P contains (includes) Q.
We will implement this in Python, but on Python you can determine if it is included. Enter the code below. Display the contents of variables ** P, Q **, and ** A ** once, and then execute.
>>> P
{1, 2, 3, 4, 5, 6, 7}
>>> Q
{3, 5}
>>> A
{2, 4, 5, 6}
>>> Q <= P
True
>>> Q <= A
False
Use ** <= ** to determine the inclusive relation of a set in Python. Since the ** Q ** element is included in ** P **, ** True ** is output, but the ** Q ** element is included in ** A **. Since some of them do not exist, ** False ** is output.
Alternatively, you can also use the ** issubset method **.
>>> Q.issubset(P)
True
This time, I touched on various set operations. There are many types of set operations, but most of them are often asked in the Information-Technology Engineers Examination. Let's hold it down.
Recommended Posts