1.15 Data Structure in Python | Python Full course 2024 #learnpythonin2024 #mlwithakansha

Akansha Yadav covers Python's four core data structures: lists, tuples, dictionaries, and sets. She explains when to use each one based on whether you need mutability, ordering, duplicates, or key-value pairs, with code examples for indexing, appending, and removing elements.
Chapters
Ladies and gentlemen, this video we will see that what are the data structure in programming. So what is data structure basically? So data structure is the fundamental concept which allows us to organize our data and to store our data. Till now we were using variable to store the things which was not sufficient to store more data. But we do have some data structure which we can use to store our data and to organize it in a certain manner, like one by one, like num— in the numeric order, or you wanted to assign some names to that particular data. So everything is there in the data structures. So these all are the several built-in data structure that we are going to use. So there are mainly the 4 One is the list, then we have tuple, then we have dictionaries, then we have set. These are the built-in data structures in Python programming. So what are lists? Lists are basically organized or ordered— basically not that much organized, I would say, because list allows us to have duplicate values. List allow us to have mutable— mutable means we can change our data, we can modify our data. So there are 4 key points to mention, uh, to note. It is, it is ordered collection, it is mutable, it allows duplicates, it accesses elements by index. So here is an example. Suppose I have a list, say 1, 2, 3, 4, 5. Then I have a which says that this is ordered collection, right? Another one which I have is the fruits, which is apple and banana. Suppose in future I wanted to replace banana with the grapes, I can do that because list is a mutable one. Another thing to note over here is the list is defined within the brackets, square brackets. List are defined within the square brackets. Allow duplicates, it means I can have multiple same values, like I can write red, red 2 times, green, green 2 times, or more than that, which means allow duplicates are allowed. You can access the element by index. Index means suppose I have a list, says num is equal to 1, 2, 3, 4, 5, 8. So this is my list. What I want is I want to print the num, and I'm printing it, right? Let's select the kernel first. I have a list and I'm printing the list and I got my entire list. Suppose I want to print only the number which is 3. I wanted to print 3. So indexing in a list starts from 0. Indexing in any data structure will start from 0. It means 1 is at 0th position, 2 is at 1st position, 3 second position. So what I wanted to do is I want my num variable to print something which is at second position, and that is how it works. So it means list allows us to access elements by index. You can access the elements using index just by using the square bracket and writing the value that you wanted to extract. It means the index value. Make sure you are starting your index counting from 0. 0, 1, 2, 3, like this. Same way you can modify the content. Suppose I want— I'm saying num , it means the value that I got at the second position, I want it to be replaced with the 13. So now let's check it by printing num. So yes, it is modified. I have Initially I was having 1, 2, 3, 5, 8, and now I'm having 1, 2, 13, 5, 8. So this is how you can modify the data. Another thing which you can do is you can remove it as well. If you can add, you can remove. That is also a plus point within the data structure. So if you wanted to add something, what you can just do is you can write num.append append is to add something at the end of the list. Append is to add something at the end of the list. Suppose I wanted to add 30, so I can say num.append 30. Now if I will print num, I will get 30 at the end of my list. If you want something at the specific position, you can use the numbers. Like I want it at 5th position and I want the number 40 to be there. So it is allowed to be. So append will help you to add any content. If you wanted to remove it, then for removing also you do have a method called remove. You will say num.remove. What do you want it to remove exactly? So whatever element you want to remove, you will write that element. Suppose I wanted to remove 30 from the list, so I have written 30. So it will say 30 for this. Okay, so this is, this is giving us one error which says that list.remove— this particular thing is not allowed because of the— that particular thing is not available in the list because we just replaced it with 40. So I will say 40. So now it found the number and now it has deleted that particular number from the list. So now I have my list back with the 5 items. So that is how the list data structure works. It allows you to have duplicates, it allows you to add or modify the content, it allows you to remove the content. You can access the content using indexing. Make sure the indexing start from 0. You are starting the indexing from 0. Another data structure that we have is the tuple. Now let's see what tuples are. Tuples are also the ordered collection of the elements. Basically, the difference between list and tuple is list is defined within the square bracket, tuples are defined using the round brackets. It means this round brackets you have to use to initialize it or to write the tuples. List was mutable, it means you were allowed to modify the content. But in case of tuples, you are not allowed to modify the content. It is immutable. It means whatever you have written, it means you have written. For example, there are the coordinates of particular location, so that is also not required to be modified. So those particular thing comes under the tuples. List is something that we use to store some random things or some, uh, situations where we are just, you know, creating a random data, or maybe we wanted to change it afterwards, just like fruits, colors, numbers, all these other things which we wanted to change afterwards. But in case of tuples, tuples are something which is stick to the point. It means write the data, but that you are not going to change in future. Coordinates, dimensions of something are immutable, so that will come under tuples. Useful for representing fixed collection. You can also access the element using indexing over here in tuples as well, but it won't allow you to change the data. If you wanted to do that, what you can do is you can change your data structure from tuple to list and then modify the content and change it back to tuple. So now I will show you a quick example of tuple So we were having one variable called coordinates in which I have two coordinates, 3 or 4, let's say. If you wanted to check what is the data type of this, you can use, say, that coordinates.dtype. Okay, we will change the way we will write it. type.coordinate, and it says tuple. I will say type bracket num, type bracket num, it will give us as a list. So this was a tuple, this is a list. So in case of tuple, you are not allowed to modify it, but you can access the element using the indexing. It means indexing is totally allowed. So whatever item I have at the 0th position, it's printing that for me. You can access or you can unpack the items as well. Suppose there are 2 items, right? So what I can do is I can say x, y is equal to coordinates. I'm writing x, y, it means now I wanted to unpack whatever I have stored in tuple. This is just for the shorter data, like if you have 1 or 2 values, then you can work like this. If I will say print x, it will print that particular value for me. Print y, it will print— it will go systematically. It means one by one. It won't say that x is equal to 4 or y is equal to 3, because in the coordinates variable I have firstly I have stored 3, then I have stored 4. So one by one it will go and it will store that particular thing and unpack it in the same manner. Another data structure that we have is the dictionary. So list and tuple was the day— it was for the thing where you have the single value data and you wanted to just store it. And but in case you have something like key-value pairs, just like if I say person's name, age, these all are the key value. It means name of the person, age of the person, address of the person, phone number of the person, like this. So this is also— this is not ordered collection, this is unordered collection. If it is unordered collection, it means you cannot access it using the indexing, but you can access this using another way. Right now you cannot access it using the indexing. It is unordered collection of key-value pair. Dictionary is equal to key-value pair. This is how you can remember the dictionary quickly. Dictionaries are key-value pairs where the key are the Names value are the John or whatever the value you are going to store. It is mutable, it means after creating it you can modify the content of it. Suppose you wanted to change cities, you wanted to change the person's name or person's age, then that is totally allowed in dictionary. But in dictionary there is one thing which is keys are unique, it means you cannot repeat the key. If you have defined a dictionary and you are using duplicate keys, it will just remove that particular thing and will take the first part. It means suppose you are writing a key 2 or 3 times, it will take the first time where you have written key, that particular key. Rest all it will remove. Access values by key. So you can access the values using key only. There is no way that you can access using indexing. Another difference is that dictionary has to be defined using the curly brackets. This was round bracket, this was square bracket, but this time it's gonna be a curly bracket. So you have to write curly brackets, curly brackets you have to use, and you will write key first of all. Suppose name is a colon John. Another one is the age key. Then I am writing age is equal to 30. That is a number, so I'm not using quotation. City, let's suppose, and I'm storing city as UK, New York, right? New York. And writing down the variable, which is dict is equal to this. So this is how you will define any dictionary. Once you have defined it, there are all the things which you can do. Suppose you wanted to access the particular thing, let's suppose I wanted to access the name of it, so I will say dict name. It will print that thing. It means I am accessing the elements using the key. You can modify the con— modify the values as well. Let's suppose mistakenly I have written 30, the age of that person was 31. So I will say age is equal to 31, and now I will try to put in the data. And now Okay, it was capital A. Yeah, like this. But earlier I just added up, so I will just run all the cells again. Yeah, now it's correct. We were having a dictionary in which we were having age variable. I tried to modify the content of age, and here we got the output. You can add any new key-value pair also. Let's suppose I wanted to say that that person's occupation is is engineer. So you can say job dict, whatever key. Suppose it's occupation, just like we did the age, a was in the lowercase, so that was added as a new key in the dictionary. So I'm saying now if I will print it, it has occupation as a new key. You can use a del command to delete whatever you wanted to delete. Suppose I wanted to delete age, and now I'm going to print it. So now we don't have age because we have deleted it using the del command. So that is how you can work with the data structure dictionary. Another data structure that, that we have in the Python programming is the set. There is some similarities between list and tuple. Same way we do have some similarities with the dictionary and the set, and we do have some differences as well. Dictionaries was supposed to be with the key and value pairs, but in case of set it is going to be the individual. It means unordered collection of unique elements. There won't be any key-value pairs, but just like dictionary, it— you will define it using the curly brackets. Sets are mutable. It means you can change the data what you have added. It is a common set operations you can perform, just like in the mathematics also. You might have learned about the union, intersection, and all. So those methods, set operations, can be performed on this set as well. No duplicate elements allowed means duplicates are not allowed. So let us see how to work with the set. Set is unordered collection, so It's gonna be the same. Let's suppose I'm writing colors is equal to curly brackets red, blue, black, red. Now I have written 2 times red, blue. Now let's see if I'm printing it, what's gonna happen. Now we don't have red, we have red but one time. But you might have noticed something that It's not ordered. Whenever I'm running the program, it's not giving me in one order, just changing the order randomly. So that is why we say it is unordered collection. If you wanted to add something or remove something, you can directly use a command called .add. And suppose you wanted to add yellow. And you're printing it. Yes, you have successfully added it. Another is the colors.remove, and I wanted to remove blue. Let's print the variable. Yeah, so we have removed and we have added as well. So that is how you have to work with the data structures.










