0% found this document useful (0 votes)
8 views

vertopal.com_setassignment

The document explains various set methods in Python, including update, add, pop, remove, discard, clear, and copy, detailing their functionalities and differences. It highlights that update can add multiple elements, while add only adds one, and compares remove and discard in terms of error handling. Additionally, it distinguishes between shallow and deep copies, providing examples to illustrate how changes in nested objects affect the original and copied objects differently.

Uploaded by

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

vertopal.com_setassignment

The document explains various set methods in Python, including update, add, pop, remove, discard, clear, and copy, detailing their functionalities and differences. It highlights that update can add multiple elements, while add only adds one, and compares remove and discard in terms of error handling. Additionally, it distinguishes between shallow and deep copies, providing examples to illustrate how changes in nested objects affect the original and copied objects differently.

Uploaded by

bragha144
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Explore, understand, and create an article covering the following set methods: update, add,

pop, remove, discard, clear, and copy. Additionally, compare these methods with those you
have previously learned.

update() : This method will allow to add multiple arguments inside the update method in a
single stroke.

set1={1,2,3}

set2={4,5,6}

x=set1.update(set2)

x={1,2,3,4,5,6}

add() : This method will add only an element to the set at the end of set and does not allow
multiple parameters unlike update method

Eg

s1={1,2,3}

s2={4}

s1.append(s2)

output :

s1={1,2,3,4}

pop() : pop allows a random element from the set to be removed and it does not follow any
indexing order unless index is mentioned.

s1={1,2,3}

s1.pop()

s1={2,3} a random element will be deleted which is 1 from s1.

remove() : This method will remove a specific element from the set by accessing a paricular
element and passing it as an argument inside parenthesis.

s1={67,87,90,900}

s1.remove(900)

output

s1={67,87,90}

discard() : This method will remove an element from set like remove method but does not
throw an error even when trying to discard non existing element from set unlike remove
which shows error.

s1={34,87,90,67,78}
s1.discard(90}

s1.discard(0)

output

s1={34,87,67,78}

s1={34,87,90,67,78}

cler() : This method is capable of clearing all elements inside a set and remains us an empty
set.

s1={45,90,80,78}

s1.clear()

output

s1={}

add() :

# add method
s1={90,89,87,88,98}
s1.add(99)
print(s1)

{98, 99, 87, 88, 89, 90}

update() :

# update method example


s1={56,98,90,99,0,89}
s1.update([97,88,77,67,70])
print(s1)

{0, 67, 70, 77, 88, 89, 90, 97, 98, 99, 56}

remove() :

# remove method
s1={78,34,90,98,79}
s1.remove(78)
print(s1)

{34, 98, 90, 79}

discard() :

# discard method
s1={78,87,98,89,79,97}
s1.discard(97)
print(s1)

{98, 87, 89, 78, 79}

clear() :

s1={90,89,98,78,87,99}
s1.clear()
print(s1)

set()

pop() :

# pop method
s1={78,87,89,98,90,99}
s1.pop()
print(s1)

{99, 87, 89, 90, 78}

copy() : This method will copy the elements of a set to another set by using copy() but address
will get changed of newer set.

Below is example

s1={99,98,97,96,91}
s2=s1.copy()
print(s2,id(s2),type(s2),sep="\n")

{96, 97, 98, 99, 91}


136785119239904
<class 'set'>

Comparison with Other Methods

Methods like add() and update() are for adding elements, but update() works with multiple
elements at once.

remove() and discard() are for deletion, but discard() is safer because it doesn’t throw errors

pop() is unique because it removes an element randomly, unlike the targeted remove() or
discard().

2. Explain the difference between shallow copy and deep copy with an example each

A shallow copy creates a new object but only copies references of the elements inside. If the
original object contains nested objects, changes in the nested objects will affect both the
original and the copy.
# let us see an example
import copy
original=[[1,2],[6,7]]
shallow=copy.copy(original)
shallow[0][1]=989
print(original,shallow,id(original),id(shallow),sep="\n")

[[1, 989], [6, 7]]


[[1, 989], [6, 7]]
136785116754688
136785117966336

A deep copy creates a new object and recursively copies all objects inside. Changes in nested
objects do not affect the original object.

# let us see an example


import copy
original=[[1,2],[6,7]]
deep=copy.deepcopy(original)
deep[0][0]=98
print(original,deep,id(original),id(deep),sep="\n")

[[1, 2], [6, 7]]


[[98, 2], [6, 7]]
136785116858432
136785116867264

You might also like