﻿ Python: Frozensets - w3resource

# Python Exercise: Frozensets

## Python sets: Exercise-13 with Solution

Write a Python program to use of frozensets.

Sample Solution:-

Python Code:

``````x = frozenset([1, 2, 3, 4, 5])
y = frozenset([3, 4, 5, 6, 7])
#use isdisjoint(). Return True if the set has no elements in common with other.
print(x.isdisjoint(y))
#use difference(). Return a new set with elements in the set that are not in the others.
print(x.difference(y))
#new set with elements from both x and y
print(x | y)
```
```

Sample Output:

```False
frozenset({1, 2})
frozenset({1, 2, 3, 4, 5, 6, 7})
```

Note:- Frozensets can be created using the function frozenset(). This datatype supports methods like copy(), difference(), intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference() and union(). Being immutable it does not have method that add or remove elements.

Python Code Editor:

```x = frozenset([1, 2, 3, 4, 5])
y = frozenset([3, 4, 5, 6, 7])
#use isdisjoint()
print(x.isdisjoint(y))
#use difference()
print(x.difference(y))
#new set with elements from both x and y
print(x | y)```

Improve this sample solution and post your code through Disqus