Help us understand the problem. What is going on with this article?

# python入門(6)~集合~

More than 1 year has passed since last update.

# 集合

## 他の型からの変換

set()を使うことによって他の型から集合を作ることができます．

set1.py
```a = set('JAPAN')
list = [3, 6, 23, 8]#リスト
b = set(list)
print(a)
print(b)
```

```{'A', 'P', 'N', 'J'}
{8, 3, 6, 23}
```

## 要素数の取得

len()を使うことによって要素数を取得することができます．

set2.py
```a = {'A', 'B', 'C', 'D', 'E'}
length = len(a)
print(length)
```

```5
```

## 値の有無の判別

set3.py
```a = {'c', 'java', 'python'}
chk1 = 'python' in a
chk2 = 'swift' in a
print(chk1, chk2)
```

```True False
```

## 集合演算

ope.py
```ob = {'java', 'php', 'python', 'c#', 'c++'}
c = {'c', 'c#', 'c++'}
script = {'javaScript', 'ruby', 'php', 'python'}
#和集合
sum = ob | c #sum = ob.union(c)
print(sum)
#差集合
sub = ob - c #sub = ob.difference(c)
print(sub)
#積集合
pro = ob & script #pro =ob.intersection(script)
print(pro)
#排他的論理和
xor = ob ^ c #xor = ob.symmetric_difference(c)
print(xor)
#部分集合
part = c <= ob #part = c.issubset(ob)
print(part)
#互いに素
a = c.isdisjoint(script)
print(a)
```

```{'python', 'c', 'php', 'c++', 'java', 'c#'}
{'python', 'java', 'php'}
{'python', 'php'}
{'python', 'java', 'php', 'c'}
False
True
```

## 最後に

Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away