Python Basics - Data Types Part 3 |

Опубликовано: 29 Август 2026
на канале: AriCodes
21
3

Set Data Types: A Set is a built-in mutable and unordered data type used to store a collection of unique items.
Denoted by set, and initialized using {} i.e. curly braces, or set().
Used when you need to quickly strip out duplicate values from an existing list.

Two types of set data types:
1. set - mutable, unordered
2. frozenset - immutable, unordered

To create an empty set we use set(), if we use {} it will simply create an empty dictionary not an empty set.

Code:
numbers1 = {4, 2, 3, 1, 3, 2, 4, 2, 1, 3, 2, 3, 4, 3, 4, 1, 4, 2, 4, 3, 4}
print (numbers1)

numbers2 = set([4, 3, 2, 1, 2, 3, 4, 2, 3, 1, 2, 4, 3, 3, 4, 1, 4, 2, 4, 3, 4])
print (numbers2)

print(type(numbers1), type(numbers2))

numbers1.add(5)
print (numbers1)

numbers2.remove(4)
print (numbers2)

numbers3 = frozenset([1, 3, 2, 4, 2, 1, 3, 2, 3, 4, 3, 4, 1, 4, 2, 4, 3, 4])
print (numbers3)

numbers3.add(5)
print (numbers3)
will give error as 'frozenset' object has no attribute 'add'.