0% found this document useful (0 votes)
8 views2 pages

SET Interview Question

A set is an unordered, mutable collection of heterogeneous values with immutable elements, where duplicates are not allowed. Key methods of a set include add, remove, discard, pop, and various operations for union, intersection, and difference. The main differences between remove, pop, and discard are that remove raises an error if the element is not found, pop removes an arbitrary element, and discard does not raise an error if the element is absent.

Uploaded by

POOJA DHAKANE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

SET Interview Question

A set is an unordered, mutable collection of heterogeneous values with immutable elements, where duplicates are not allowed. Key methods of a set include add, remove, discard, pop, and various operations for union, intersection, and difference. The main differences between remove, pop, and discard are that remove raises an error if the element is not found, pop removes an arbitrary element, and discard does not raise an error if the element is absent.

Uploaded by

POOJA DHAKANE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

SET

1. What is set?
Ans: Set is a comma separated values within curly braces. Set is unordered, Mutable
and it is collection of heterogenous values of immutable elements where duplicates
are not allowed.

2. 2. WHAT ARE METHODS OF SET?


ans:
1. add(element): Adds an element to the set.
2. remove(element): Removes an element from the set.
3. discard(element): Removes an element from the set if it exists.
4. pop(): Removes and returns an arbitrary element from the set.
5. clear(): Clears all elements from the set.
6. union(set2): Returns the union of two sets.
7. intersection(set2): Returns the intersection of two sets.
8. difference(set2): Returns the difference of two sets.
9. symmetric_difference(set2): Returns the symmetric difference of two sets.
10. issubset(set2): Checks if the set is a subset of another set.
11. issuperset(set2): Checks if the set is a superset of another set.
12. isdisjoint(set2): Checks if the set is disjoint with another set.
13. copy(): Returns a copy of the set.

3. WHAT IS DIFF BETWEEN REMOVE AND POP


Ans:
Remove():
1. Removes a specific element from the set.
2. Raises a KeyError if the element is not found in the set.
3. Requires the element to be specified.
s={11,22,33,44}
[Link](11)
print(s) #{33, 44, 22}
#Remove method gives value in return 'NONE'

Pop():
1. Removes and returns an arbitrary element from the set.
2. Raises a KeyError if the set is empty.
3. Does not require specifying the element.
s={11,22,33,44}
[Link]() # It deletes value randomly
print(s) #{11, 44, 22}
print([Link]()) #it give deleted value in return

4. Difference between BETWEEN DISCARD AND Remove?


Ans:
Remove():
1. Removes a specific element from the set.
2. Raises a KeyError if the element is not found in the set.
3. If element exists, removes it.
# ##REMOVE() Method
# '''Syntax: [Link](value)'''
s={11,22,33,44}
[Link](11)
print(s) #{33, 44, 22}
#Remove method gives value in return 'NONE'

Discard():
1. Removes a specific element from the set.
2. Does not raise an error if the element is not found.
s={11,22,33,44}
[Link](111)
print(s)
print("coding")
{33, 44, 22}
coding
#It doesnot give error if element is not their.

You might also like