Python Sets
Python Sets
Set
Sets are used to store multiple items in a single variable.
Set is one of 4 built-in data types in Python used to store collections of data, the other 3
are List, Tuple, and Dictionary, all with different qualities and usage.
Example
Create a Set:
Try it Yourself »
Note: Sets are unordered, so you cannot be sure in which order the items will appear.
Set Items
Set items are unordered, unchangeable, and do not allow duplicate values.
Unordered
Unordered means that the items in a set do not have a defined order.
Set items can appear in a different order every time you use them, and cannot be referred to by
index or key.
Unchangeable
Sets are unchangeable, meaning that we cannot change the items after the set has been created.
https://www.w3schools.com/python/python_sets.asp 1/4
7/15/2021 Python Sets
Once a set is created, you cannot change its items, but you can add new items.
Example
Duplicate values will be ignored:
print(thisset)
Try it Yourself »
Example
Get the number of items in a set:
print(len(thisset))
Try it Yourself »
Example
String, int and boolean data types:
Try it Yourself »
Example
A set with strings, integers and boolean values:
Try it Yourself »
type()
From Python's perspective, sets are defined as objects with the data type 'set':
<class 'set'>
Example
What is the data type of a set?
Try it Yourself »
https://www.w3schools.com/python/python_sets.asp 3/4
7/15/2021 Python Sets
Example
Using the set() constructor to make a set:
Try it Yourself »
https://www.w3schools.com/python/python_sets.asp 4/4