+1 vote
in Programming Languages by (60.0k points)

If I want to initialize an empty set, I can use set(). But when I initialize a set with some values, it returns an error. How can I initialize a set with some values?

>>> a=set(2)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'int' object is not iterable

1 Answer

+2 votes
by (354k points)
selected by
 
Best answer

You can use curly brackets to initialize a set with some values in Python. 

Here is an example:

>>> a={2}

>>> a.add(4)

>>> a

{2, 4}

>>> a.add(4)

>>> a

{2, 4}


...