# Python Sets (With Examples)

Learn to create and use sets in Python 3 with examples.

Sets are an ** unordered collection of unique and immutable objects**. It supports mathematical operations from the set theory.

An item appears only once in a set, no matter how many times it is added.

### Topics Covered

**How to create a set?**

You can use the `set()`

construct with iterables or use curly braces

to create a set in Python.**{}**

**Code**

```
# Empty set
x = set()
print(x)
# Using iterables
x = set("Pylenin")
print(x)
x = set([1, 2, 3, 4])
print(x)
# Using curly braces
x = {1, 2, 3, 4}
print(x)
```

**Output**

```
set()
{'l', 'y', 'e', 'P', 'i', 'n'}
{1, 2, 3, 4}
{1, 2, 3, 4}
```

You cannot declare a set by using empty curly braces. Python will assume its a ** dictionary**.

**Mathematical operations with sets**

**Set membership test**

You can use the

operator to check if an element exists in a set.**in**

**Code**

```
x = {1, 2, 3, 4}
if 1 in x:
print("Element exists")
else:
print("Element doesn't exist")
```

**Output**

`Element exists`

**Difference between 2 sets**

In mathematical set theory, the difference between two sets(A & B) is

. It is the set of all elements of A that are not elements of B. You can perform the same operation in Python**A - B**

**Code**

```
x = {1, 2, 3, 4}
y = {2, 3, 4, 5}
print(x - y)
print(y - x)
```

**Output**

```
{1}
{5}
```

**Union of 2 sets**

The union of two sets A and B is the set of elements that are in A, B, or in both A and B.

**Code**

```
x = {1, 2, 3, 4}
y = {2, 3, 4, 5}
print(x | y)
```

**Output**

`{1, 2, 3, 4, 5}`

**Intersection of 2 sets**

The intersection of two sets A and B are the set of elements that are in both A and B.

**Code**

```
x = {1, 2, 3, 4}
y = {2, 3, 4, 5}
print(x & y)
```

**Output**

`{2, 3, 4}`

**Symmetric difference of sets**

The symmetric difference of two sets A and B are the set of elements that are in A and B, but not common to A and B.

**Code**

```
x = {1, 2, 3, 4}
y = {2, 3, 4, 5}
print(x ^ y)
```

**Output**

`{1, 5}`

**Using methods with sets**

**Adding elements to sets**

To add elements to a set, you can use the

method.**add()**

**Code**

```
x = {1, 2, 3, 4}
x.add(10)
print(x)
```

**Output**

`{1, 2, 3, 4, 10}`

With

method, you can add only a single element. To add multiple elements, use the **add()**

method.**update()**

**Adding multiple elements to sets with update()**

**Code**

```
x = {1, 2, 3, 4}
x.update([4, 5, 6, 7])
print(x)
x.update(["Pylenin", "Python"], {"greeting"})
print(x)
```

**Output**

```
{1, 2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 5, 6, 7, 'Python', 'Pylenin', 'greeting'}
```

Duplication is always avoided with sets.

**Remove an element from a set**

To remove an element from a set, use

method.**remove()**

**Code**

```
x = {1, 2, 3, 4}
x.remove(2)
print(x)
```

**Output**

`{1, 3, 4}`

If the element you are trying to remove doesn’t exist, Python will throw a

.**KeyError**

To avoid getting such errors, use the

method with sets.**discard()**

**Code/Output**

```
x = {1, 2, 3, 4}
x.discard(20)
print(x)
>>> {1, 2, 3, 4}
x.remove(20)
print(x)
>>> KeyError: 20
```

**Remove multiple elements from a set**

To remove multiple elements from a set, use the

method.**clear()**

**Code**

```
x = {1, 2, 3, 4}
x.clear()
print(x)
```

**Output**

`set()`

### Problems

- Write a Python program to check if a set contains one or more items that are False.

**Hint**- Use any() method - Write a Python program to return all the unique elements of a list.
- Write a Python program to return the common elements between 2 sets.
- Write a Python program to check if there are any common elements between any 2 lists.

**Hint**- Use the intersection concept of sets.