set

set(iterable)

Creates a set.

Parameters

The set() function accepts one optional parameter:

  • iterable (optional) — a sequence (string, tuple, etc.), a collection (set, dictionary, etc.), or an iterator object to be converted into a set.

Return Value

The set() function returns:

  • an empty set if no parameters are passed
  • a set constructed from the given iterable

Examples

# empty set
print(set())

# from a string
print(set('CAR'))

# from a tuple
print(set(('n', 'k', 'b', 's', 'i')))

# from a list
print(set(['s', 'i', 'k', 'u', 'c']))

# from a range
print(set(range(7)))

# Result

set()
{'R', 'C', 'A'}
{'n', 'i', 'k', 'b', 's'}
{'s', 'i', 'u', 'k', 'c'}
{0, 1, 2, 3, 4, 5, 6}