** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
>>> a = {1, 2, 2, 3, 4, 4, 4, 5, 6}
>>> a
{1, 2, 3, 4, 5, 6}
>>> type(a)
<class 'set'>
I used {}
in the dictionary type, but this time it is not a key and value, but a set type.
At this time, the overlapping elements are combined into one.
>>> a = {1, 2, 3, 4, 5, 6}
>>> b = {2, 3, 6, 7}
>>> a - b
{1, 4, 5}
>>> b - a
{7}
ʻA --bremoves" a and b "from" a ", "A and b" are removed from "b" by
b --a`.
>>> a = {1, 2, 3, 4, 5, 6}
>>> b = {2, 3, 6, 7}
>>> a & b
{2, 3, 6}
>>> a = {1, 2, 3, 4, 5, 6}
>>> b = {2, 3, 6, 7}
>>> a | b
{1, 2, 3, 4, 5, 6, 7}
◆a ^ b
>>> a = {1, 2, 3, 4, 5, 6}
>>> b = {2, 3, 6, 7}
>>> a ^ b
{1, 4, 5, 7}
"A or b" minus "a and b". Those that are only in either a or b.
+
>>> a = {1, 2, 3, 4, 5, 6}
>>> b = {2, 3, 6, 7}
>>> a + b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'set' and 'set'
ʻA + b` will result in an error.
Recommended Posts