Python Program to Create a Set

Write a python program to create a set. In this example, we create an empty set and print the same. Although both look correct, the first one returns the dictionary, not an empty set.

# Create an Empty Set

s1 = {}
print(s1)

s2 = set()
print(s2)

Empty set output

{}
set()
>>> 

In this Python example, we created different sets using both the {} and set() function.

# Create a Set

s1 = {2, 4, 6, 8, 10}
print(s1)
print(type(s1))

s2 = set(['USA', 'China', 'UK', 'Russia', 'India', 'France'])
print(s2)
print(type(s2))

s3 = set("Tutorial Gateway")
print(s3)
print(type(s3))
Python Program to Create a Set 2

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.