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.

A set is a collection which is unordered, unchangeable , and unindexed.

Untitled


Set operation

len() function

fruits = {'apple', ' orange', ' banana'}
print(len(fruits)) #the length of the set

prints,

3

Elements in a set

fruits = {'apple', 'orange', 'banana'}
for items in fruits:
    print(items)

prints,

banana orange apple

Adding elements to a set

fruits = {'apple', 'orange', 'banana'}

#add allows a single element to add
fruits.add('mango')

#update allows multiple elements to add
fruits.update(['pineapple', 'strawberry'])
print(fruits)

prints,