How are sets created in Python?

QuestionsQuestions8 SkillsProPython Data StructuresSep, 16 2025
096

In Python, sets can be created using curly braces {} or the set() constructor. Here are a few examples:

Using Curly Braces

# Creating a set with curly braces
my_set = {1, 2, 3, 4}
print(my_set)  # Output: {1, 2, 3, 4}

Using the set() Constructor

# Creating a set using the set() constructor
my_set = set([1, 2, 3, 4])
print(my_set)  # Output: {1, 2, 3, 4}

Creating an Empty Set

# Creating an empty set
empty_set = set()
print(empty_set)  # Output: set()

Note

  • Sets are unordered collections of unique elements.
  • If you try to add duplicate elements, they will be ignored.

Feel free to ask if you have more questions!

0 Comments

no data
Be the first to share your comment!