filmov
tv
#17 Python Tutorial for Beginners | Swap 2 Variables in Python
![preview_player](https://i.ytimg.com/vi/3dpJrMtxYeo/maxresdefault.jpg)
Показать описание
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
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
Комментарии