Swapping Variables Without a Third

Category: Python Basics

Write a Python program that swaps the values of two variables without using a third variable. The program should take two integers as input and output the swapped values.

Example:

Input: 

a = 5 

b = 3

Output: 

b = 3

a = 5

Try using arithmetic or bitwise operations to perform the swap.

a = 5
b = 3
a = a + b
b = a - b
a = a - b
print(a, b) # Outputs: 3 5

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!