Difference between tuple and set

0

I'm working with python and I was wondering what would be the difference between a set () () and a tuple (). I know that both are delimited with '()'.

But I tried to assign a set attribute to a tuple, that is:

THIS IS MY SET: (OR WHAT I THOUGHT IT WAS ...)

basket = ('apple', 'orange', 'banana', 'pineapple', 'pear')

I GIVE THE FOLLOWING COMMAND:

basket.discard(2))
print (basket)

When I ran the program gave an error ... It said:

* line 16: basket.discard (2)

AttributeError: 'tuple' object has no attribute 'discard' *

Why did you consider my set to be a tuple?

    
asked by anonymous 19.11.2015 / 20:57

1 answer

1

Because for python the parentheses delimit a tuple, not a set. If you want a set , you need to use the keys ( { , } ) or the set function:

basket = {'apple', 'orange', 'banana', 'pineapple', 'pear'}

or

basket = set(('apple', 'orange', 'banana', 'pineapple', 'pear'))
    
19.11.2015 / 21:09