Transcript:
Hey, what’s going on, guys? Welcome to another video in this video. I’m going to talk to you guys about an interesting concept that is very crucial to understand the difference between lists and tuples in Python and it is called mutation. Right if you are a beginner in Python. And if you haven’t heard about this concert, you better know it now, because when you’re going to learn about less than topics in detail, if you’re not aware of this little property, you’re going to get a lot of headaches with it, and because although this is made as a convenient feature in Python for beginners who are used to other programming languages and out that this is a massive source of I take and put errors. All right, okay. We thought meat more than me talking about. Let me actually write a small code on explaining us what it means. Let’s just say that. I have a list over here and I have some list of scientist with me. You can save chemists as well, but as in Matt, I’m just going to paste it. I have a few scientists. Names like Charles Boyle and it love Isis Henry Pascal. I guess, and Charles London, Robert. Boyle, actually sorry my bad okay now. I have four signed list of scientist’s. Names with me cool. Now what I’m going to do is let me create a copy of this. Alright, and now if I were to print wirelessly. Well, what will happen is this? I will be predicating and a copy of Y, and it’s coming out as it is all fine. Now, let’s just say. I need to add one more pound or one more scientist to this list, and I want. Let’s see Thompson. I said, let’s say there’s a scientist. Cut down soon. I think it’s a boy in Ps1. JJ. Thompson, anyway. We want to do like this and in favor to run this well. This is not surprising. Thompson is also added to why because we are pending. Why now here’s the kicker. What if I just printed out dice? Okay, and let me print why Allah, why side by side? Okay now! What happen is this? What will happen is this can happen did. Y X also got changed now. This is speared! If you’re a beginner and this is something you have to keep in mind. This is actually mutation in action. So what does mutation mean is that it means that it means that ability to change data That’s about it, so if you have a data set like there’s some kind of data like this, and you have a variable associated with it and when you make a copy as a copy of it, each of these copies are not actually separate data types. Rather these are some are just alternate names or aliases, radiuse’s the same data, right, So if I were to append why it’s not, it’s going to up Y has a Y if I happened. Y is going to modify the original data if it happened. X is called a modify. The original data, right, both of them are accessing the same entry. I mean, in the same memory location. So if I want to confirm that I can use this function. I’d that we learnt no one of the previous videos. We checked it out the idea of X and the idea, Foy. If they are different data types, let’s say these should be different. I mean, if they have a mornay different memories. However, if you look at it, the memory locations of X and Y are exactly the same, meaning Y and X are actually pointing towards the same data type. Which is this there you have it? This is called mutation in action, right so every time you a list list like this anywhere to create copies of it. You need to make sure you need to make sure that, yeah. You have to make sure that allowing mutation on or not right, the other hand if you want to break. This is one possibility you can do what you can do is you can say create a new one and then say. X DOT copy account, copy A y1 equals X dot copy. Now if I were to print now if I were to print this and paste it X, and then Y one, let me see what what do I get now if I look at it? The memory locations are different, although granted, they look very, very similar. The memory locations are different right now. If I were to run this code again. The memory locations are they are very close, but the memory locations are quite different. And that’s about it that way if you were to happened. Y one, let’s say Y one that happened and I’ve written. I had another scientist over there, fool or something, all right, and I print out X and y1 X and y1 X will remain as it is because we’re not modifying it, but y1 is the new copy and we’re working out with it. So this is one way you can prevent mutation from happening. Sometimes this mutation is helpful and you can let it be edited. Be as it is, but sometimes mutation is a pain and there you should not allow it, right, That is where tuple’s come in. If you have a list like this, and you want to pass and pass this this to some function or want to use it in some other case and list value should not to find all of that. You staples over there? Right, that’d be you. You make a list that you can’t change the data or you can’t change. Its memory location and all of that, right that way you can avoid so – so remedies to avoid, so if you have to say remedies to avoid mutation, one of them is that you stuff is instead or before sending a data or list or something. Use a couple and do something about it or do reassignments. What do I mean by that? Yes, now if I were to know what I mean by that is if I were to. Let’s say okay, forgot to create another one. Let’s say X 1 equals 1 2 3 and I say Y 1 equals 1 equals X 1 Obviously, obviously, x1 and y1 would be the same, But if I were to say, X 1 equals 4 5 6 And I print out x1 comma y1 Check, what do I get check? What do I get when I say? X1 and print, x1 and y1 Now they have different entries right now now. This is obvious now. This is kind of quietly obvious this way you are. We broke the mutation and now both of them have both y1 and x1 have different entries earlier. What happened is x1 had a particular memory location. So if I want to print it, you want to print. Let’s say initial. I’d, our x1 is you can say Id of x1 Okay, and then what do I do? I just copy this paste. It initial idea of y1 and then I’ll say copy this paste, it paste this twice finish and then a final final idea of x1 and then find the ID of y1 you know, this is kind of an overkill, but it’s worth doing it once and let me comment out these lines right now, and also this one. I don’t want it and these two also. I don’t want it right now. If you had to run this code check, what do I get? So The initial idea of x1 is is something initialize my oneness this so far so good that’s anticipated, but the moment. I change the idea of x1 I mean, I set a new value for x1 Look, what happens the idea of x1 actually changes and that, thereby you have a different. I’d memory a memory location, whereas the final memory location. Excuse me, there’s a final memory location of y1 remains as it is. And there you go. This is one way of avoiding mutation. Other way is actually simple you have to say. Y 2 equals to put of X so X 2 us X 1 or something. That’s another way to avoid it. And there you go and now, you typecasting. You’re actually creating a new data type and putting it over there, thereby up, thereby changing everything there you go. This is one of the reasons why mutation is helpful at the same time it can be a real pain, so keep that in mind and use mutations wisely. That’s it, that’s all. I have for this video. Thank you for watching in the next video. We will talk about something rather interesting and till then see you.