#13 Python Tutorial for Beginners | Swap 2 Variables in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back aliens my name is 720 and in this video we'll see how do you slap two numbers in Python so let's say we have two variables a and B the value for a is 5 and the value for B is 6 how do you swap these two values so normally in different languages we have different way of implementing it but one thing is common in most of the language which is you have to use a third variable to swap them because you cannot simply say a equal to B and B equal to a it will not work right example if I try to print the value of a and B now we are expecting that it should play into 6 and 5 of the swapping but don't mean to learn this code you can see we call it 6 and 6 that means we lost the value of 5 so it is not exactly swapping it now what's happening here the thing is the moment you say a equal to B that means the value for B which is 6 goes into a and you lost the value which is 5 we wanted 5 right we wanted to store 5 somewhere and that's why before assigning the value B to a what you will do is it will take the value of a and you will put that in a temp variable and you can use anybody about name doesn't matter so what you will do is you will take a you will save that a in a temp variable and at the end you will not assign the value of A to B because of the as6 now so you will assign the value of 10 to be so you are storing the value of a to temp then you are copying the value from B to a and at the end you're simply saying 10 is I mean B is equal to 10 it will swap the value let's verify and you can see we caught the value which is 6 and 5 so value for a was 5 not is 6 there are you 4 B was 6 now it is 5 so I think is done but then there is a problem you're wasting a variable right and in most of the languages we have different techniques but in spite of we have amazing way of doing this and we will see that at the end of the video but then let's try to simplify it can we use it in a better way instead of using our third variable normally when you go for the introduced they might ask you this question how would you swap two values without using a third variable so in this case what you will do is you will simply use of formulas and chef using a third variable you can use a formula here which is a is equal to a plus B and you will say B is equal to a minus B and a is equal to a minus B now it doesn't matter from where we got this formula but it works right example if you run this code you can still see we call it 6 & 5 so to understand how it works so let's say what is 8 plus so a is 5 and B 6 of a plus B is 11 and here at this point the value for a is 11 right because we have updated so the new value for a is 11 the value for B is 6 so the new value for B becomes 5 so the value for a is 11 the value for B is 5 and now we have one more so here the value for a is Levin the value for B is 5 the new values right and the value for a becomes 6 and you can see we got a strapped value the value for a is 6 the value for B is 5 we have done it we still have one issue with this the issue is you know if you go in deep the amount of bits required for 5 is 3 bits right so 1 0 1 is 5 and 6 is basically double 1 0 you deploy 3 bits for 500 require 3 bits for 6 but what about 1111 needs 4 bits that means you're still wasting one extra bit I know it's not a big number but then still you're wasting a bit and that's why we have one more way which is X all so you can simply use a cap which is a XOR symbol now Excel works on different way so what you can do is you can just go to Google and you can find how XOR works and you would find the answer the advantage of X or is it will not waste extra bits ok so that means this thing if the initial values are 3 bits the output will also be up 3 bits you're not wasting extra memory then you can still see the output you go you've got 6 and 5 so yes there are different ways of strapping so we got 20 variable we can use operators like + - or you can use this cap symbol which is X or basically we have one more which is amazing about Python and which is if you want to swap two values you can simply say and this this is not that you know the language okay this is very unique to Python so you can simply say echo of B is equal to B comma a so this thing doesn't work in two different lines but it works in one line how this works let me just run this code and you can see 12v got 6 and 5 so swapping is done but how it is working so let's start to understand how it works so basically when you say B is equal to a right so normally you're the right side will solve first so your system will solve the flight byte first so it will solve B comma a the value for B is 6 the value for a is 5 this goes into stack so we have a stack it was into a stack and then it reversed so we have a concept of rotation so it will rotate these two values so the value of a comes to B and the value of B comes to a and those things will be a sign here it is just rotating the value or in simple terms you can imagine a guess the value of b and b guess the value of a but it behind the C it was a different way it uses a concept of two rotation or rotation to so you can simply go to Google and search for rod two in Python right you will get the idea so even this works so let me know in the comment section if you have any more questions and so that's it from this video I hope you enjoyed swapping off two numbers see you in the next video
Info
Channel: Telusko
Views: 932,318
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python
Id: 3dpJrMtxYeo
Channel Id: undefined
Length: 4min 52sec (292 seconds)
Published: Tue Oct 23 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.