Python - Create empty Set
Python - Create an empty Set
To create an empty set in Python, call set() built-in function and pass no arguments to the function. The set() function with no arguments passed creates and returns an empty set.
In this tutorial, you shall learn how to create an empty set in Python using set() built-in function with examples.
Example
In the following program, we create an empty set using set() built-in function. We assign the empty set to a variable mySet
.
Python Program
# Create an empty Set
mySet = set()
# Print the Set
print(mySet)
Output
set()
Now, let us add some elements to the set, and print the set.
Python Program
# Create an empty set
mySet = set()
# Add elements to set
mySet.add('apple')
mySet.add('banana')
print(mySet)
Output
{'apple', 'banana'}
Summary
In this tutorial of Python Examples, we learned how to use set() method to create an empty Set in Python, with the help of well detailed example programs.