Sets in Python

Sets are used to store multiple data in a single variable. But it is not like lists, tuple or an array. The main primary difference in set is the data/items are not in a ordered or sorted order. The data will change its order everytime you refresh the page.

As the sets are unordered, so its obvious they wont have index values for them.

Sets in python are written by using curly braces.

Syntax:

setname = {"value1","value2","....","valueN"}
Example: 

fundaSet = {"funda","of","web","IT"}

We can print the whole set at once by just writing the print(fundaSet). We can also print individual values using the for loop as shown below.

fundaSet = {"funda","of","web","IT"}

print(fundaSet)
print(type(fundaSet))

for x in fundaSet :
    print(x)
Output for the above code will be :
{'of', 'web', 'funda', 'IT'}
<class 'set'>
of
web
funda
IT

As we already studied that sets are unordered, We can see that the fundaSet's order is changed in the output. Let us run the program once again and check the output.

{'web', 'funda', 'IT', 'of'}
<class 'set'>
web
funda
IT
of

Here again the order of the set is shuffled.