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

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

Udemy Courses:

For More Queries WhatsApp or Call on : +919008963671

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

Subscribe to our other channel:

Donation & Support:
PayPal Id : navinreddy20
Patreon : navinreddy20
Рекомендации по теме
Комментарии
Автор

Sir are there any notes of yours u are really osm and my last helper..

binodkumarbinodkumar
Автор

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!🙃

gitanjalidas
Автор

I'm studying Python in a Master course and Telusko is better than my professors, keep doing it please!

full_disclosure_now
Автор

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

NitinJainNoida
Автор

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!!!

fccy
Автор

Another way to swap-
a = a*b
b = a/b
a = a/b
👍👍👍👍
By the way big fan...

sahilprajapati
Автор

If your confused like me how to use phycharm then just go to tutorial 17 it's all about how to use the software

kashishchadha
Автор

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...

ashishdash
Автор

Revisited after a long gap. Great lectures

nairunni
Автор

i have been learning alot from your videos sir way of teaching is awesome, learnt more in 13 videos than in any other year

adityabhambhani
Автор

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.

mayankjulah
Автор

I learned The Xor swapping here Tq for that Navin Sir

Finally in Telusko nenu Telusukunna

vineethkumar
Автор

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!

kyle.winter
Автор

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!

shivanishrivastava
Автор

Please give us a lesson to work with pycharm environment.we r not able to get u

kakarlasundarganesh
Автор

I find also different way swaping
a=a*b
b=a/b
a=a/b
Nice video 😃

rajvaibhavtawadkar
Автор

a=a^b
b=a^b
a=a^b
sir aap konsi duniye se aye ho
never thought of using xor in this way

ShivamPanchbhai
Автор

Thanks bro seriously....it was damn good even for a beginner!!!

vidhyavarshiniprakash
Автор

sir because of you i learned python very fast

aravindamatha
Автор

He keeps calling us aliens, making me wonder what planet he is from.

mikemcconeghy