#13 Python Tutorial for Beginners | Swap 2 Variables in Python
Вставка
- Опубліковано 21 жов 2018
- Spring and Microservices Live Course : bit.ly/springmslive
Coupon: TELUSKO25 (25% Discount)
Industry-Ready Java Spring Microservices Developer Live : bit.ly/JavaMS2
Complete Java Developer Course : bit.ly/Comp-Java-Dev-2
Coupon: TELUSKO20 (20% Discount)
For More Queries WhatsApp or Call on : +919008963671
website : courses.telusko.com/
Instagram : / navinreddyofficial
Linkedin : / navinreddy20
TELUSKO Android App : bit.ly/TeluskoApp
Discord : / discord
In this lecture we are discussing about different ways to swap two numbers:
-- suppose we have two variables a and b and we want to swap the value of a and b
-- we can do this by directly assigning the value of b to a and a to b lead to loose of value of a and b
-- we can do this by using temp variable
-- we can do this by using xor operator
-- we can do this by using python technique
directly assigning leads to loose of value
a=5
b=6
a=b
print(a)
6
print(b)
6
a=5 # resign the value of a to 5
use temp variable for swapping
temp=a
a=b
b=temp
print(a)
6
print(b)
5
#without using third variable we can swap the value
a=a+b
b=a-b
a=a-b
print(a)
5
print(b)
6
#use of xor operator for swapping
a=a^b
b=a^b
a=a^b
print(a)
6
print(b)
5
in python we have another technique
a,b=b,a
print(a)
5
print(b)
6
Github :- github.com/navinreddy20/Python-
Python for Beginners :- bit.ly/3JOLQhl
Java and Spring Framework For beginners with Spring Boot : - bit.ly/3LDMj8D
Java Tutorial for Beginners (2023) :- bit.ly/3yARVbN
Check out our website: www.telusko.com
courses.telusko.com
Subscribe to our other channel:
Navin Reddy : / @navinreddy
Telusko Hindi : / @teluskohindi
Donation & Support:
Indian Payment : www.instamojo.com/@NavinReddy/
PayPal Id : navinreddy20
Patreon : navinreddy20 - Наука та технологія
Sir are there any notes of yours u are really osm and my last helper..
I'm studying Python in a Master course and Telusko is better than my professors, keep doing it please!
I feel like i am in a class. So good sound, english, way of learning and teaching... Wow, i didnt expect so much! You're awesome! Keeo rocking!🙃
To calculate XOR of two numbers, the thumb rule is, "If the two digits are same then put 0 else if they are different put 1 ". For.e.g calculate xor of 10 and 20. So the binary of 10 and 20 is shown below and the calculation
01010
10100
-----------
11110 which is 30
Thanks
Thanks man
Cool....thanks 🤘🏻👍🏻
Whoa thanks
thanks
how it will works for xor of 10 and -20 ??
thank you so much! i just spent half an hour trying to understand my professor's analogy for variable swapping. this was straightforward and comprehensive, thank you!!!
Another way to swap-
a = a*b
b = a/b
a = a/b
👍👍👍👍
By the way big fan...
Its should be
a = a*b
b = a//b
a = a//b
Then u will ans single number .. without .0
yes o/p comes in float. Need to use //(Integer division) to get correct o/p if using integers.
But you are wasting 1bit
If your confused like me how to use phycharm then just go to tutorial 17 it's all about how to use the software
Thank you
thanks kashish
Thank you
Thank you
To use pycharm ,open it , make a new project, right click on blank screen, click on create and choose python file. Then open it. Type urncode and use shift+f10 to run it.preyty simple
When i saw this in java i was confused that how this formula just worked. I was confused about swapping.. But now all that stuff is cleared.. Thanks to naveen bro...
i have been learning alot from your videos sir ......your way of teaching is awesome, learnt more in 13 videos than in any other year
You are really awesome sir teaching way is excellent, while iam studying final year iam fearing about python course but when I saw ur videos it's very easy 😁
I was searching high and low for a good explanation of swapping variables with a third variable. This was by far the easiest to understand!
Thank u so much sir😃ur the best teacher of programming.ur all videos are excellent and understandable....and all videos are helpful.once again thanks for making all those videos☺️
wow, this swapping concept is amazing. Thanks Navin for this
I learned The Xor swapping here Tq for that Navin Sir
Finally in Telusko nenu Telusukunna
Wonderful the concept of swapping variables is really amazing👌. You have explained with different concepts to execute this and it's clear to me now specially the rot_two(). Thank you very much for such efforts❤
He keeps calling us aliens, making me wonder what planet he is from.
alien means unknowns
xD
Because we're new to Python world 🙄
@@kapila112 yeah good point
@@kapila112 true 🤣🤣🤣🤣
great, now i can simply copy this technique to finish my computer homework that our computer teacher has given us. Thank you wise man.
Thanks bro seriously....it was damn good even for a beginner!!!
Your way of teaching is amazing
This was very informative. I did not know that Python was this good
Very nice teaching sir.....i am really enjoying....
Thanks for these informative vdos
You are far better than my subject teacher..👍🏻
this is the best python tutorial i have ever seen
Amazing ...you make it so simple...u have deep knowledge❤️
For those of you wondering how he got the pycharm ide go to video no. 17 @5:40
I have been loving the videos and learning a lot. At this point I think what is occurring for me is that I have learned the lessons up to this point but how do I use what I have learned? What would I be using it for? How do I get it to stick without application and repetition? Help!
Please give us a lesson to work with pycharm environment.we r not able to get u
Yeah
Same bro....dont know how to use pycharm...its very confusing
@@tharunkumar9208 same here bro plz tell us how to use pycharm
Go to tutorial 17
Superb explanation...you are the best among all which i have seen..
Can you please share a notes or pdf if you have
sir because of you i learned python very fast
It's a really helpful for me.... thank you sir😇
You haven't explained how to use Pycharm in any of your previous video and you're using it in this video without even telling the fundamentals of it. It's not fair.I am sure a lot of viewers (Including me) won't be able to get this and will find it confusing. I myself have wasted a lot of time just trying to figure out Pycharm. So please take care of all this stuff.
a mechanical engineer is learning python bcaz of u, thank u gurujiiiiiiiiiiiiiiiii
A CA student learning Programming Because of Him. Respect for this MAN !!
Hi, Navin
I didn't understand why there's formula for swaping and that's clearly doing iteration adding/substracting making it one another step!
Very good explanation...short vedios are creating intrest to watch more.Thank you
great video sir i am able to learn very easily from this
Dear sir, I think you should give guidance regarding how to create or familiar with pycharm.
This was super helpful, thank you!
a=a^b
b=a^b
a=a^b
sir aap konsi duniye se aye ho
never thought of using xor in this way
Welcome to the alien world :)
well your method is good but is not accurate , your answers will be in decimal if you use your method. Example, if a = 36 and b = 56, then you use your method your answer will be 56.6 for a and 36.6 for b but you can correct it by typing = int(a) which is 56 and int(b) which is 36
Great video no one ever taught me that xor method
Sir your teaching is good,,🌹🌹
Sir we can use "a, b = b, a" also... It works
.
.
Big Fan🙏
Right
Thanks for explaining variety of swapping
Sir, your mental thinking is unbeleavable
use a,b=b,a
you are smarter then him
Bro u r Magician
it's easier than you think, you can just fool the client by doing this:
print('The value of a is '+str(b))
print('The value of b is '+str(a))
osm bro....I tried it :)
So simple ;)
Thank you very much. You are a genius.
Excellent video. Pl clear the concept of "wasting 1 bit"
To understand the 2nd method at 1:50 I thought of a story.
Just imagine that you have two rods and also two stickers called "A" and "B".
You take the rod with a length of 5 and paste the sicker "A" on it.
Then you take the other rod with a length of 6 and paste the sicker "B" on it.
Now you join the two, which is with a length of 11, you leave the sticker "A" on it and remove the sticker "B".
Now you cut away the pole from the joint and paste the sticker "B" on the side of the length with 5.
You leave the sticker "A" on the side with a length of 6.
If you could visualize this you will understand this method easily. Keep it up!!!
Awesome as usual
Sir
.u r videos r awesome 👌👌👌👌👌👌
I find also different way swaping
a=a*b
b=a/b
a=a/b
Nice video 😃
If any of a and b is 0 then this logic don't work
Well explained bro....
Nice explanation :)
Generally I am not interested to give a comments but this time after seeing this video I can't keep myself calm 🔥
thank you for making these videos.
lmao the Thumbnail already showed me the answer to my questin thx man
how can't this channel have billion subscribers
coz this is not T-Series
🤣🤣
And ur way of talking is really cool😎
could u pls make a video on how python different from another language (By using Functions)
those who intrested pls like and reply this
Thank you so much sir
This really helped in alot
sir, explain concept of rotation with example
Amazing tutorial sir,
I enjoy a lot ..😁😁
Awesome teaching
very much helpful, thank you.
Hello sir.. Just curious..
If we take 2 input from user
n1 and n2
and without doing any operations we just print
First
print(n2)
print(n1)
Will it work na..
And can pass all the test cases and also
Time and space complexity will also be less..
Please correct me if am wrong...
1.Are you running this program in pycharm? 2.what is the valid interpreter in pycharm while running a program?
you rock and keep teaching
keep making videos like this sir
Your explanation is very well sir..I'm very thankful for your lectures☺☺
If==> a,b,c=1,2,3
a,b,c=c,a,b=b,a,c=c,b,a
print(a,b,c)
What will be stored in a,b,c?
I'm waiting for your answer sir
I also need explanation sir
Thank you...!!
New (a, b, c)=(2.3.1)
@@labdhogranagasamudram4364 would you pls explain the procedure🙂!
@@keerthanareddy8856
Actually I'm not able to txt the compilation process here😅
The value changes according to the nxt =...
Or else if u want u can mail me I'll send the pic of my compiled one
@@keerthanareddy8856 actually sry for late😂
@@labdhogranagasamudram4364 ur mail I'd??
Well explained 👏 👌
This a+b method or XOR method works for only integers. How to do it for strings?
I have got one way using lists without using third variable and python way of a,b=b,a but want to understand from others too.
@Naveen Sir, Please guide me on this.
Unbelievable it's all free.
You are amazing🙌🙌
when you do (a,b) = (b,a), wont the value of b be updated resulting on getting the same value again? or is it working because we have applied it in same line so that the value of a swaps with b and with out updating the original value of b, a gets the original value of b. is it how its working? -_-
my first python project
the concept in this video was quit difficult because you did explain,,,,,, but not deeply.......
thanks...very useful...
Sir add game tutorial as well...it will cover more area..also it will be fun to learn..!!!!
not for class 2 -_- even im in 8th and hate that idea
But XOR also performs the same function as addition in the first line of formula that means it adds a and b. So , how it uses less number of bits. Can you explain
it simply does not perform the same operation. search for XOR swap in wikipedia
Navin can you give quick tutorial to switch to pycharm. thanks
Hey Navin, another great video! I just do not understand why you did not speak two lines about XOR(exclusive OR) which is like a binary operation involving three bits only for your example.
Thanks sir for this information🙏
Thanks Sir🤩
Could you plz explain about stack and quea in detail?
smart one!!
what is the use of programming in pycharm ??? We can also type this program in python also.... then for what we are using pycharm. We can use python itself for this?? Or we have to do in pycharm
print ("ANOTHER METHOD FOR SWAPPING TWO VARIABLES IN PYTHON")
a=5
b=6
a=a*b # a = 5 * 6= 30 a : 30
b=a/b # b=30 / 6= 5 b : 5
a=a/b # a=30 / 5=6 a : 6
print(a)
print(b)
Output:
6
5
Hi Naveen, could you please show us to use "temp" for swapping 3 variables
It depend on you then in what manner you want to swap....
If a=1
b = 2
c= 3
For a=2, b=3 , c= 1....
Then lets swap as follows:
temp = a
a= b
b = c
c= temp.....
Similarly in any manner you can do ....
@@rahulpareek7450 How can we use the formula for three variables??
@@rahulpareek7450 ya this is right we can swap the variables this way.
@@moodybaalika3102 a=1,b=2,c=3
a=a+b+c
b=a-b-c
c=a-b-c
a=a-b-c
You will get a=3,b=2,c=1
Sir please make a video on how ROT_TWO works.
import numpy as np
a=3
b=6
temp=a
a=b
b=temp
print(a,b)
sir i did understand this video where did you do the coding
thank you very much!
hey buddies...start working on juypter notebook...its the simplest❤..As a complete beginner i am quite comfortable with it.
Thank you very much
very very good
Can we swap two no. By multiplying and dividing as it is in 'c' ??
Need to know xor in detail and how to use exactly here.
nice explanation