Skip to content
C

Swap Two Numbers Without a Third Variable

Mediumpython

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>

Input (stdin)

Output

Run your code to see output here...