Swap Two Numbers Without a Third Variable
Read two numbers and swap their values without using a third temporary variable, then print the values both before and after the swap.
What the problem means: normally, swapping two variables needs a helper variable to hold one value temporarily. Python lets you do it in a single line without one.
Approach: read a and b, print them, then swap using tuple assignment: a, b = b, a. Print them again.
Input: Two lines: the first number a, then the second number b (both whole numbers).
Output: Two lines: Before swap: a = <a>, b = <b> After swap: a = <b>, b = <a>
10 20
Before swap: a = 10, b = 20 After swap: a = 20, b = 10
- -10^9 <= a, b <= 10^9
Hint 1
Python can assign to multiple variables in one line: a, b = b, a swaps them directly.
Hint 2
Print the "before" line first, then swap, then print the "after" line.
Python evaluates the right-hand side of a, b = b, a fully before assigning, so it naturally swaps both variables in one step — no temporary third variable required. Print the values, perform the swap, then print again.