Day-9 Python + MYQL FREE Sessions | Codingdidi

Codingdidi covers Python's collection types: tuples (immutable, with indexing and unpacking), sets (unordered, no duplicates, with add/remove operations), and dictionaries (key-value pairs where keys must be immutable). The instructor walks through syntax, common mistakes, and practical use cases like fixed configuration data and coordinate pairs.
Chapters
Hello everyone, how are you all? Yeah, am I audible to you? Yes. Okay. All right, so let's get started. I'm going to share my screen. Let me know once it is visible to you. Is it visible?
Yeah, it's visible.
Okay, so before we begin with anything, do you guys have any question related to anything that you have like maybe revised or watch previous recording, anything to ask? Yeah, anyone, any question, or shall we begin with today's topic? All right, so our— in the last video we discussed about— in the last basically session we discussed about list, that what exactly list is. If I wanted to change any item, then how to do that, and many more things we have discussed. Now we are going to discuss the next thing in Python, which is built-in data type again in Python, which is tuple. So tuple is basically again used to store multiple items, but in tuples it is unchangeable. You can't change anything within tuple. This can also hold multiple data type. And this is immutable, you can say, or you can say unchangeable. So to create tuple, you can use constructor, or else you can use the round brackets. When you wanted to, uh, go with the— you wanted to get the values, you have to follow the same indexing method. So items in the tuples are indexed. Whenever you are going to insert anything, that will have— that will hold one index value, will begin from 0 and Like multiple values can be there, right? Another thing about tuple is that tuple allows duplicate values, okay? Same like list, but the only difference is that you cannot make modification in the data that you are going to insert in the tuple. So let's just quickly create one tuple. So let's say t is my variable and I'm going to create a tuple. So let's see, and I'm going to just print t. So now you can see t is my tuple. I'm going to check the data type with the type function and it's going to say tuple because I have created tuple with the round brackets. This is how we have to create tuples. When you want individual value to be printed, then you have to use the method of indexing. So this will help you print one single value. This will help you access one single value at a time. You can also use negative indexing in this. Like if you want the last item, then you can just mention -1 and it will display the last item from that particular tuple for you. You can use slicing method as well. Like if you want 2 items, 3 items, then same way you can extract the multiple items as well. But when it comes to the point that you wanted to make changes in the tuple— so as I said, tuples are immutable, but we know that list is something which is changeable. It means we can make changes in the data which is available in the list. But with tuple, this is not possible. So there is an alternative, that is that what you have to do is whenever you wanted to make changes in the tuple, you have to change it to the list, and then you have to make the changes and then convert the list back to tuple. 3 steps. First is you have to convert the tuple to list. Second step, you have to make the changes in the list. Third step is then you have to convert the list back to tuple. Is this clear? Or you have any question in this?
Converting to the list means like by using constructors?
Yes. Okay, using constructor we have to convert a tuple to list and list to tuple. Okay, if I talk about the use cases, like why we have made tuples as unchangeable, so it is something like for the configuration of the system, you can see. Right? It has the fixed configuration. We can't make any changes in the configuration of the system. Or you can say, on map we have coordinates, right? So that would be the best example for tuple, because in coordinates we can't make changes. We, we cannot. This is the fixed coordinates, right? If you are making changes, it means the location itself will change. Okay, so now I will show you how exactly you have to do it. So first is we already have a tuple which is t. Now we have to convert it into list. So you can use variable and you can say list— sorry, t is the variable which I am looking forward to convert it in the list. Now if I'm going to say print l, this is going to print the entire list. Now suppose instead of a I was supposed to make it b. Which is the first one. First, A, I wanted to make it B. So I can use the method indexing is equal to— I want it to be B. And I will run this. Now I will just check it whether the value is updated. Yes, the value is updated. Now I have to convert it back to tuple. So to convert it back to tuple, t is equal to tuple of L. And now if I will print t, it will be with the updated value. So 3 steps you have to follow. First is convert tuple to list. Second is you have to make the changes in the list. Third is convert it back to tuple. This is how you can make changes in the data of tuple. Got it? Any question? No, ma'am. Okay, now let's talk about when we wanted to add some items in the tuple, then can we add it or not? So as I said, tuples are immutable, so adding an item is not valid, like you can't do it basically within tuple. So for adding item in the tuple also, you have to convert it to list and then you have to use the append method and then you have to append it and then convert it back to tuple. But suppose you wanted to add two tuples, I said you cannot basically update it, right? Basically, you cannot make the changes in the content, or you cannot add more values in the tuple. But when it comes to two different tuples which are already created and already stored in your system's memory, so then at that time you can basically add those two tuples if you wanted to do it like that. Like, you can say that this is an alternative if you wanted to add any item to the list. Okay, so you have to add that particular item. Let's say t is already my variable, and now I wanted to— in this particular list, I wanted to add apple. Okay, when I wanted to add apple, is like we have to create a new tuple for that. So I will just create t1 is equal to apple. Now I have t and t1, and I want t tuple to have this value apple. To add it in the tuple, you can simply use the plus operator. So this will add it up basically. Okay, so it goes like t plus equal to t1. You can write— what I did— t is our previous variable. And then I have written— yeah, so you have to write it like this. Now I will show you the t variable first. Okay, now you can see we have apple added in the First tuple. When do you have to use this? Like, I wanted to just add Apple, but I have added one extra comma. This is how you can add it. If you're not going to add that, you might encounter some errors in this. Okay, just like I have shown you on my screen as well. Okay. Any question in this?
If we don't give comma, then we will get an error after this Apple comma.
Yeah, that is because when you are not going to add a comma, right, so that particular value will be considered as a string, right? Because we have written just the single value within quotation. Single value within quotation means that is a string. When you are trying to create a tuple— tuple means when you have more than one values, obviously, right? If I'm going to just say a is equal to apple, if I'm saying a is equal to apple like this, both ways, like a and a, both a are having same value, single value. When it comes to single value, your Python tries to identify— Python tries to detect that, okay, This is single value, single word. It means this is a string. When we are adding a comma, it means we are trying to say that more items. Maybe in future we will add more items and we— I want it to be considered as a tuple. Got it?
Yes.
I will just show you. Let's say if I'm writing type of a, you can see it's showing string. Now if I'm going to say type of capital A, which is my second variable, it will be considered as a tuple. But now let's say I'm writing b is equal to apple, okay? And I'm going to say type of b, it will be considered as a string if you are not going to add a comma, okay? So make sure if you wanted to— if you are writing even though in the brackets, you have to add a comma to indicate Python that maybe in future I will add more items, but you have to consider it as a tuple only. So parentheses are not enough to just define tuples. You have to use commas to indicate that you are going to have multiple values and multiple data types can be there. Got it?
Yes. Like for example, in the, uh, like we have one tuple, right? Like a T. For that we need to add like a capital A means, uh like t is equal to a plus— like that we need to do, correct?
This one you are saying?
Ah, yes, sorry.
Yeah, plus—
okay, yeah.
So you have to write— you can write it like this also: t is equal to t plus t1. Okay, right. But we are using this operator we have discussed already, so to make this particular statement shorter, okay. So like, I don't have to repeat T multiple times. I'm just directly saying in the T variable, you have to basically add T1 and store it back to T.
Okay, okay, yes.
Any other question? You have to make sure that whenever you are defining any data type, so you have to make it clear according to the kernel as well. So just like I, as I told you, that if suppose in flow flow, like you just forgot that this concept, that adding comma, right? So obviously it will give you an error saying that you cannot add string to the tuple, right? So right from there itself, like whenever the error will be printed, you should understand what error is trying to say, okay? So whenever you will say— whenever you will get to know that, okay, this is exactly the error, okay, and it is saying that you cannot add string to tuple, so now I have to make changes. Somewhere I have added a wrong data type, so I have to check the data type there, okay. So that's how you can update any tuple by converting one data type to another data type and then making the changes. Now let's say you wanted to remove something. So for removing, again, if we wanted to make changes— tuples are unchangeable, you cannot do it. If you still wanted to do it, same workaround is that you convert it into list and then make the changes and then convert it back to tuple. That's the same workaround that we have just discussed. But let's say you wanted to delete the entire tuple itself. So when it comes to deleting entirely tuple, like completely you wanted to delete tuple, go with the del statement. So let's say I have the tuple A, capital A, 1. I will just show you how to delete it. So write del space A and run this. Now if you will check print A, it will give you name error because you have deleted the tuple. There is no tuple like a variable a. Variable a doesn't exist, basically. Okay, now let's say you have a tuple Let's say coordinates. Okay, coordinates is the variable. How many coordinates do we have in a plane? Yeah, anyone? You know about coordinates like x comma y?
Like that is the one, right?
Okay, yes, yes. So in a coordinate, like if we are talking about the 2D surface, then we have two coordinates, x and y, right? So let's say I am creating one coordinate variable and I'm going to add two values. Let's say x value is 15 and second value is, let's say, 20. So coordinates is equal to 15, 20. And if I'm going to run this and I will check the data type of coordinates, it will show as a tuple only because I'm using the round brackets and I have multiple values within it, so it will be considered as a tuple. Now let's say I have to work on the data where I need to work on the x-axis separately and I have to work on the y-axis separately to make just some analysis, or maybe anything it could be. But now my task is to unpack it. I want the x data to be in the different variable and y data to be in the different variable. So how will you do that? One way is you directly go and write it like this, but right now I just have one single variable coordinates. Obviously when you are working on the data you will have lakhs of rows, millions of rows maybe. So at that time you have to use the method of unpacking. Okay, for now I'm just going to show you how you have to do it with the single one. So as your, as your tuple have two values, so it means you need two variables. So you can write x comma y is equal to, and then you have to mention the variable that contains those two values. It's like assigning multiple values at once. Same way we are going to assign values from tuples to variables. Now if I will run this, my variable x will have the first value and my variable y will have the second value. This is how we unpack any tuple. Any question in this?
Um, if it is more than 2 variables, like, um, how we can unpack?
Same way. You have to write down the variable, the number of— like, obviously, if suppose I have a data sheet, okay, in that data sheet I have 10 columns, so I know that there are 10 columns. Right, so I have to write 10 different variables over there. I want entire data is there in the TXT format, let's say, and I know that values are separated by comma in the TXT format. But now I have to convert those TXT format into an Excel sheet. When it comes to Excel sheet, in Excel sheets we have columns, but in terms of TXT file, which is a Notepad file, we don't have columns, we just have a plain area to write it. Now I have been told that, okay, the values are separated by comma. So I know that, okay, I will ask the next question: how many, uh, values do you think that txt file consists of? So Lamsam, they told that, okay, there are 10 values, right? So obviously I have to write 10 different variables. I can write like a, b, c, d, like totally depends upon the situation, like what will be the variable name. I'm just giving you an example. You have to just write down the variables like this. And then you have to add equal to, and then you write down the txt file, whatever the file name is. And then if you will run, and it will show you that how many arguments are missing. Let's say they gave you the wrong data, they told 9, but correctly there were 10 values. So you can immediately correct it by writing one more variable. Got it? Okay, yeah, so with the indexing it will work, I think. Yeah, that's it with the tuple. Now we will learn the next one, which is set. When it comes to set, set is similar to tuple, similar to list, but there is no specific order of element. So in set, the collection is unordered. Second thing is set won't allow duplicates. Third thing about set is set is mutable. It means you can add item or you can remove it as. So when to use set? Set is used when you wanted to store unique items, when you wanted to perform some operations like you wanted to collect the common data between multiple sets, or you wanted to check for the data which is different data from multiple set. So there only you can use set. Okay, so set is immutable, but you can add or remove the items. Okay, second thing about set is it is unordered. No duplicates are allowed. Okay, and another thing to notice, it can store multiple data types. Okay, so these are the features about set: unordered, unchangeable, and unindexed. To define set, you have to use— suppose S is my variable— you have to use curly brackets. Over here you have to mention the values. Type of s. Okay, so now you can see this is my set, this is the data type. Now when I just executed the set, you can see that my first element was a, but in the result I'm getting true. So it means it is unordered, it is— there is no particular order. Okay, and it is also unindexed. It means there is no indexing, there is no order in set. Whatever you are going to add will be jumbled anytime when you're going to execute it, it will be in the jumbled format only. So unordered means it is not going to have any defined order. It will appear every time in the different, different views. You cannot access anything with the indexing because it doesn't have indexes as well. Sets are immutable, but in case of set, when it comes to the word immutable or unchangeable, meaning that you cannot change the item after the set is created, but if you still wanted to add any item or remove any item, that you can do. Okay, you cannot change it existingly, like whatever items has been stored, you cannot change it, but apart from that, if you wanted to change it, you can. Another thing to note in set is that when you are writing the value true and false— true and false are Boolean data, right? When you're trying to store Boolean data, true and false, in the set, and at the same time you have the data like 0 and 1— 0 and 1 and true and false. So in terms of set, true and 1 are considered as the same value. Therefore it will, you know, it will go with the duplicates and will store only one value out of true and 1. Same with the false and 0. False and 0 are considered as the same value and they will be treated as a duplicate value. Therefore, any one value will be stored in the set. Let me give you example. Let's say s1 is my variable and I'm going to store multiple values. Okay, I have stored 1 also, 0 also, and I will just add some duplicate values as well. 96, let's say, and 9. Now I'm going to create one more variable s2 in which I'm going to store again similar set of values, but this time I'm going to change the position of the 0 and 1, and I'm going to add it later on. Okay, now I will just run this. Now I'm going to print s1. When I am printing s1, you can see that above, when I was creating it, I have added multiple values, but in results I'm getting very limited. Why is it so? First thing is everything is jumbled. Second thing to note is I have written 1 first, right? 1 was in the— 1 was before true, so therefore 1 has been considered and true has been removed. 0 was in the beginning, therefore 0 has been selected, but false has been removed. As I said, there cannot be multiple values, duplicate values. So 1, it was repeated, that is why we don't have that value. And whichever value out of set1 is like was repeated, that has been removed, and only unique values are stored in set1. Now what I am going to say, set to— then I— we have already copy pasted the entire data, but this time we will have true and false. Because you can see from the beginning, if you are going to notice, true was placed before 1 or 0, 1 or 0. Same false was placed before 0. So that is why true and false has been selected, but 1 and 0 has been neglected. Any question in this? Anyone, any question in this? As we don't have anything like indexing in Set, so you cannot access the item directly. Using indexes or anything anyway. Like, there is no workaround even to get the access to any single element from the set. But you, if you want, you can iterate it through loop and you can get anything from the— suppose I wanted to check whether 9 exists in my list or not, so I can use the identify operator. I can say that, okay, print 9 in set 1. If it exists, it will say true. If it doesn't, then it will say false. So that's how you can check it whether that particular value is present or not, but you cannot access it. You cannot even change sets. You have to— you cannot, basically you cannot change. Instead of 9, if you want 10, you cannot do that. But as I said, it is immutable, but you still can add items or still can remove items. When it comes to add items or removing items, we have a function called add. So suppose in S1 you wanted to add something, you have to use add function. And let's say I wanted to add OK. So now if I'm going to say print S1, it will print the updated set 1 with the value OK. When you wanted to merge two different set, let's say I have S1 and I have S, and I wanted to merge them, I wanted to combine them. So there is one more function that you have to use, which is update function. So which one do you want it to update? If, if s is the primary one and you wanted to update s, so you will write s first and then you will write down that function. So s.update, and then you will write down the variable which is the second one, which is your second set, and you will run it. Now if I am going to say print s— sorry, So this is my updated set where I have combined two different set and I have created one single set. You can even update set with any data type. Suppose you have one list, let's say I'm going to create L2 is equal to 3 values. Okay, now if I wanted to update my set, I can still do it no matter what data type it is. What I did, we have written Wait, let me just— do we have any variable which we have just executed? We have it over here, which is L. And then we are creating L2. Oh, sorry, I'm using the wrong function. It should be update. Yeah, we are using update. And then you will get it over here. If I'm going to print s, then you can see the values has been added. 67, then we have 75 and 46. So that's how you can update it. Basically, when you are using the update function— add is used basically when you wanted to add any single value or any item to the set, you can add it. But you, when, when you're combining two set, or when you're combining, when you're trying to update your set basically with any data type, like it can be, it can be list as well, it can be the tuple, it can be dictionary as well. So any, any iterable item it is, then you can use it. Now let's talk about when you wanted to remove it. As we know that it doesn't have any indexing. When it doesn't have any indexing, it means you can use the remove function. So remove function— in remove function, you have to mention the value. There are two functions basically in set which you can use. One is remove, another one is discard. When you are going to apply remove function, so remove function means you have to mention the value. Let's say I wanted to remove certain things, so you will say s.remove bracket open and you will mention the element. It can be any element. So let's say I'm writing OK and I'm going to run this. It will work because OK exists in the variable. And if I'm going to run this particular cell, it will be without OK, right? Now I'm going to run the cell again. If suppose that value doesn't exist from within your set, it doesn't exist, then it will give you KeyError. If it is giving you KeyError, it means that a value doesn't exist. But at the same time, if you are going to use discard, discard function is not going to give you any error. So discard bracket, you have to mention the element At the same time, it won't give you any error. If that value exists, it will delete it. If it doesn't exist, it will give— it will, you know, run that cell without any error. Okay, suppose you wanted to delete one random item from the list, so directly you go with the pop function, s.pop, and you will run it. Whatever has been deleted will be printed for you, 96. But this is the random value. I am not mentioning which value to remove, but just a random value. You can use this if you wanted to just clear everything. You have the clear function. And if you wanted to delete the entire set, same way you will go with the del. Del S1, let's say. So it will delete it. Now if you will try to print S1, it will say name error because we don't have S1 in our notebook. This was all about set. Any question in the concept of set or tuple? Anyone, any question? No. If you want to access any, uh, any value, how can you access? Directly you cannot access any value from set, as I said. If you still want to do it, let's say, so you can go with the for loop. You can say for i in s, print i. Okay, while it will continue printing all the values and you wanted to check, or maybe you wanted to see some values, or you want, suppose, whenever the 69 will occur, do this. So you directly go with the for loop and make it happen. Okay, okay, yeah, that's how you can do it. Or you can apply if-else function over here. You can say that, okay, if i is equal to 67, print it, otherwise don't print other value. So maybe you wanted to just access i at 67, so you can just directly write down the for loop with the if-else and you can do it. Okay, okay. All right, so now let's talk about the last one, which is the built-in data type, that is dictionary. The dictionary— as I have already given you an introduction about dictionary, that how dictionary looks and how to create dictionary. So dictionaries are also supposed to be created within curly brackets, and they are the key-value pairs. Now, few things to note about dictionary is that dictionaries are unordered. Just like set, dictionaries are unordered. In dictionary, keys are unique but values can be duplicated. It means suppose if I am saying name, so name cannot be repeated anywhere as a key. But if I have values, in terms of values, if I'm saying values like suppose if I'm adding students name, so those students name can be repeated, but that name word cannot be repeated. So that means Keys are unique, but values can be duplicate. Second thing is keys must be immutable, but values can be mutable. It means when you're writing keys, you cannot change it, but when it comes to value, you can change it. You can add anything in dictionary, you can remove anything from dictionary, you can update the existing values as well. In the dictionary because everything is based on key-value pairs. So right from there itself, like from the dictionary data type itself, you are not going to deal with the indexes. You are going to deal with the keys. When you want any item, you have to call keys. When you want any values, you have to use keys. Keys are the only way to access the values from the dictionary. So now let's create one dictionary. Suppose student is the dictionary name, and I'm opening a curly bracket. Let's say name. As I said, my values can be multiple, so I'm using square bracket because I'm going to add multiple values in the dictionary. So suppose I'm going to add the participant's name in this. So 3 values I have added. Okay, now I'm going to add comma. This is my first key-value pair. Now I've added comma. Age is my second key, let's say, and I'm going to add keys again. Obviously, if I have multiple values for name, I have to add in further suits. Just random age I have added. Now let's say— now this is my dictionary where we have 3 keys and multiple values are there. All the values which I have stored is in the dictionary— is in the list format. Now I'm going to change it for just for a change, just to show you that you can use multiple data types within this. So first value is in the— what I have done— okay, now it's correct. Name, which is within the list. Age, which is in the tuple. And languages are in the set. Now if I'm going to print student, it will display my entire key-value pair for me. If I just wanted to get the list of names, so I have to use in the keys, okay? So print student, what do I want? I want keys name. Now it will print all the names for me. Suppose you wanted to make changes in any of the data, you can make the changes using keys. For an example, let's say I just want the value— I, I just, I don't want entire data to be printed, I just want only keys to be printed. When it comes to keys, we have common methods in Python which you can use. So it is like print student.keys. It will print all the keys. You can see these are the 3 keys in our dictionary. If when it comes to values, we have values variable and we have— you can see these are the values. Key-value pairs together are known as item. Okay, so you are go— if you want the item pairs, then you will go with this function. And you can see name, and this is the value. This is second item, this is third item. Got it? Any question in this? Yeah, anyone, any question? Okay, so for today what you have to do is you have to practice all these things related to tuple, set, dictionary, and list. And in the next session we will discuss something Okay, all right guys, bye-bye, take care. Thank you.










