filmov
tv
Python Tutorial for Beginners | Swap two variables in Python

Показать описание
Please don't forget to like and subscribe
'''
Swap two variables in python
1. Tradional Method
2. Mathmatical Method
3. Using XOR operator in python
4. Pythonic Way of swapping
'''
x = 5 # 3 bits
y = 6 # 3 bits
# print(x,y)
# temp = x
# x = y
# y = temp
# print(x,y)
print(x,y)
# x = x + y # 4 bits
# y = x - y
# x = x - y
#using XOR operator.
# x = x ^ y # 3 bits
# y = x ^ y
# x = x ^ y
# Pythonic way of swapping
x, y = y, x
# x, y = 6, 5
# assignement (because of '=')
print(x,y)
'''
Swap two variables in python
1. Tradional Method
2. Mathmatical Method
3. Using XOR operator in python
4. Pythonic Way of swapping
'''
x = 5 # 3 bits
y = 6 # 3 bits
# print(x,y)
# temp = x
# x = y
# y = temp
# print(x,y)
print(x,y)
# x = x + y # 4 bits
# y = x - y
# x = x - y
#using XOR operator.
# x = x ^ y # 3 bits
# y = x ^ y
# x = x ^ y
# Pythonic way of swapping
x, y = y, x
# x, y = 6, 5
# assignement (because of '=')
print(x,y)