Tutorials

0


Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference. 
Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {};
Code segment:
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} print(basket)

Output:

{'orange', 'banana', 'pear', 'apple'}
The above output shows that duplicates have been removed.

Membership Testing Sample Test:

Code Segment:
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print('orange' in basket)
Output:
True

Post a Comment

 
Top