Using add and update methods

Using add and update methods

Lesson Details:
September 17, 2020

Video Transcription: In this lecture I am going to tell you about add and update methods which are used with sets this video is brought to you by Appy Pie’s Academy, so in order to show that let me write a new program press the right mouse button on the project name go to new then python file and let me give it any name suppose let the name be set to okay so now I am going to take a set so I am defining a set I have written numbers is equal to within curly braces as you know the values of sets are given within curly braces so I would write 10 20 30 so this is creation of a set and then I'm going to add a value into this set by writing numbers dot add okay so let me give the value suppose 40 so numbers not add 40 the 40 value will be added into this already defined set numbers as you know sets are immutable that means we cannot modify the values but we can very well add add elements into a set so you let me give the comment because I have added a new value I will write in the comment adds the value 40 okay then I have printed the set numbers in numbers press the right mouse button on the program name click on run and this is the output as you can see now there are 4 values earlier there were 3 values only at the time of creation into the set numbers so now I would tell you that how to use the update method always remember one thing that update method is used when we are supposed to add multiple elements for adding single element we people use add method only so I have what I am going to do is I am going to take a list and I would be adding that list into this so I have written values is equal to 100 200 and 300 so values is our this is our list and what I am going to do is I'm going to write numbers dot update values now what will happen is in the already created set numbers the list values will be added so because the list is containing multiple values we were supposed to use update method so it adds multiple values of what update methods does is adds multiple values or a list or another set so I will give you an example how to add a set into a set also okay before that let's see this this was the list values which was having these three values 100 200 and 300 so these three values they they have been added into the set numbers now in order to execute the program you can also press the right mouse button on the program itself and then click run so this is the output you can see the values of the list they have been added into our set now I will show you an example whereby I will be adding a list I will be adding a set into the set numbers so what I'm going to do is I am going to take a set with the name nums his name could be anything then within curly braces I have given the value 1 2 3 so let me give the comment also creation of another set ok so what I will do is I would write numbers dot update and within parentheses I would write nums okay so what will happen is into the set numbers the set nums will be added then I would print the numbers so the the set numbers is ever-increasing we have first there were three values then we had added the value for T and now we are going to add another set called numbs into it run the program yes as you can see now it is containing ten values total number of ten values so this is how we can add a set into another set so always remember add method is to be used when we are going to add only one value into the set and update method only when we are going to add multiple values into the set.

loader
Course content