Python Core



Admission Enquiry Form


Sets in python

Sets are used to store multiple items. Sets are unordered, so you cannot be sure in which order the items will appear. It cannot store duplicate values.

 

Example of Sets

myset={"Python","C","C++","JAVA"}
print(myset)



Output

{'C++', 'Python', 'JAVA', 'C'}



Example of sets using duplicate values

Duplicate values will be ignored.

myset={"Python","C","C++","JAVA","Python"}
print(myset)


Output

{'JAVA', 'C++', 'Python', 'C'}



Example of sets storing integers

myset={10,20,30,40,50}
print(myset)


Output

{50, 20, 40, 10, 30}



Example of sets storing float values

myset={10.23,23.45,8.21}
print(myset)


Output

{8.21, 10.23, 23.45}



Example of sets storing values of different types.

myset={10,"Python",34.54}
print(myset)


Output

{10, 34.54, 'Python'}



Example of joining two sets together

myset1={"Python","C++"}
myset2={"Java","Digital Marketing"}
myset3=myset1.union(myset2)
print(myset3)


Output

{'C++', 'Digital Marketing', 'Java', 'Python'}