Creating and accessing Sets

Lesson Details:
September 17, 2020

Video Transcription: In this lecture I am going to show you how to write the basic coding of sex this video is brought to you by Appy Pie’s Academy, okay I'm going to I have already created a project called my set so I'm going to create a Python file in it let the name be set 1 so set 1 dot py file will be created so I will start with the comment and the comment I am going to give is following is a simple set creation okay so this is going to tell us how to create a set in the simplest manner so I am going to write like values is equal to and see carefully I am using curly braces I am using curly brackets I am not using either square brackets or parentheses so you can see all the values of this set 10 20 30 100 200 they have been put inside curly braces so that indicates that this is a set now I will use as usual I will use the print statement to print all these values let us execute the program and see the output okay so this is the output hundred two hundred ten twenty thirty these values these are the values which have been picked up from the set called values okay so let's proceed further so now what I am going to do is okay let me have another set I'm going to give it a name record so what I am going to do is I am going to prove that the values in a set they can be heterogeneous so a set can be heterogeneous that means it can contain values belonging to various different data types so record is equal to 1 0 1 camel Mohan 175 as you can clearly see in this set there are three values the first one belongs to the integer datatype second one belongs to the strength Ord one belongs to a floating-point data type so heterogeneous values are cloud so let me execute the program and see the output yeah the output is being shown the string will be displayed first so okay now the next thing we are going to do is we will be we can not we will learn that we cannot have duplicate values which I have already discussed in the slide about sets so we cannot have duplicate values in a set so listen it carefully if we have duplicate values in a set it's not going to give us any error message but it will print only one value out of all those to placate values let us illustrate this so here I am having set with the name numbers within curly braces placing all these values under 200 900 etcetera and you can clearly see value hundred is being repeated it appears how many times four times one two three four yeah four times so when I will print this set numbers no error is going to come but the thing that we will see is the hundred value hundred will be printed only once yeah as you can see why because set does not allow to have duplicate values all the values they are to be unique right so next what I am trying to do is I'm going to tell you that we cannot have mutable elements in a set right as we have already understood that mutable values are not allowed in a in a set because in a set all the values are immutable so as you know list is mutable and tuple is immutable set is also immutable here what I am trying to do is I'm going to have a set called roll numbers whereby I have got values within curly braces 1 0 1 2 0 2 3 0 3 and after coma I am having the value of certain values 10 20 30 within square brackets as you know square brackets means I am trying to have a list over here which will not be allowed it will be erroneous because the list is mutable mutable elements are not allowed so I will try to execute this program as you can see there is an error let's go upwards see unhackable type this kind of error will appear on the screen on hashable type column list so this is not allowed okay right but in the same breath I want to tell you that had it been or had it been a tuple they wouldn't have been any problem because tuples are not mutable they are immutable so let me copy and paste and instead of using square brackets let me use parentheses so with parentheses it will become a tuple so this error will go so I am writing okay sorry for comments I should have given hash symbol right okay so this is a tuple because we have written the values within parentheses so tuples are allowed because they are immutable so immutable values are allowed in the set because set itself is mutable so it's a very interesting thing okay so we are going to print our set called rule numbers let's execute the program and here you can see the proper output is being displayed right the values of the tuple are getting printed 10 20 30 and the other values which belong to the set that is 1 0 1 2 0 2 & 3 0 3 they too are getting printed.

Course content