Social Media

Image Slider

Showing posts with label Set. Show all posts
Showing posts with label Set. Show all posts
1/09/2019

pop, discard, remove methods of Set Data Structure

pop() function: This function will remove any random element from the set.
It will return that element which is removed by it.
If set is empty this function will give error (KeyError).
s1={1,2,3,4}
s1.pop()
print(s1) # {2,3,4}

discard() function: This function will remove the specified element from the set.
The return type of this function is None.
s1={1,2,3,4}
s1.discard(2)
print(s1) # {1,3,4}

There is no error come if the specified element is not present in the set or if the set is empty.

remove() function: The return type of this function is None.
It will give error (KeyError) if the specified element is not present in the set or if the set is empty.
s1={1,2,4}
s1.remove(2)
print(s1) # {1,4}

copy() function: This function is used to copy whole data of one set to other set object.
s1={1,3,4}
s2=s1.copy()
print(s2) # {1,3,4}

Note** List cannot be an element of set.
Tuple can be an element of set.



1/08/2019

Methods related to Set

add(): This method is used to add any new value to set data structure.
s={1,2,3}
print(id(s))
s.add(4)
print(s)
print(id(s))


Both id will be same, this proves that set is mutable data structure.

update() function: This function actually useful when we want to add all the elements of a sequence or more than one sequence into a single set.
s={1,2,3}
s.update([4,5])
print(s) # {1,2,3,4,5}

We can also pass more than one sequence in the update method.
s={1,2,3}
l1=[1,4,5]
l2=[6]
s.update(l1,l2)
print(s) # {1,2,3,4,5,6}


intersection() method: The return type of intersection method is a set. The simple example of usage of intersection method is given below.
s1={1,2,3}
s2={3,4,5}
s3=s1.intersection(s2)
print(s3) # {3}


union() method: As suggested by the name it will perform union operation between the two sets. Its return type is also a set.
s1={1,2,3}
s2={4,5,6}
s3=s1.union(s2)
print(s3) # {1,2,3,4,5,6} 


issubset() method: This function will tells us whether a given set is a subset of targeted set or not. Its return type is boolean.
s1={1,2,3}
s2={1,2}
print(s2.issubset(s1)) # True


issuperset() function: This method will tells us whether a given set is a superset of targeted set or  not. Its return type is also boolean.
s1={1,2,3}
s2={1,2}
print(s1.issuperset(s2)) # True



clear() function: This function is used to remove all the elements from the set data structure, means it will empty the targeted set.
s={1,2,3}
s.clear()
print(s) #set()


1/04/2019

Set Introduction

Set is a data structure in Python which is denoted by {}.
Duplicate values are not allowed in set.
We cannot use indexing and slicing here.
It is mutable data structure.

Creation of empty set:
s=set()
print(type(s)) # set
Note* s={} is not empty set. It is dictionary.

Set does not preserve any sequence of storing data elements.
s=set("mayank")
print(s) # {'n', 'm', 'a', 'y', 'k'}

Set can only take sequence as argument.
s=set([1,2,2,3,4]) # 2 will store only once as repetition is not allowed here. 
print(s) # {2,1,4,3}