Python set()

Python set() builtin function is used to create a set object in Python using elements from iterable object.

In this tutorial, we will learn about the syntax of Python set() function, and learn how to use this function with the help of examples.

Syntax

The syntax of set() function is

set([iterable])

where

ParameterRequired/OptionalDescription
iterableOptionalA python iterable object whose elements will be used to create a Set.

Returns

The function returns object of type set.

Example

In this example, we will create a set from an iterable, say list, using set builtin function.

Python Program

myList = [2, 4, 6, 8]
mySet = set(myList)
print(mySet)
Try Online

Output

{8, 2, 4, 6}

Create Empty Set

If we do not pass any iterable to set() function, then set() function returns an empty set.

In the following program, we will call set() function without any argument.

Python Program

mySet = set()
print(mySet)
Try Online

Output

set()

Conclusion

In this Python Tutorial, we have learnt the syntax of Python set() builtin function, and also learned how to use this function, with the help of Python example programs.