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!
