Method 10: Comparison of all swap methods Dry Run in PYTHON

Method 10: Comparison of all swap methods is an interactive PYTHON dry run visualizer from the Io programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Method 10: Comparison of all swap methods Program Code

# Comparison of all swap methods
x, y = 10, 20
print(f"Original: x={x}, y={y}\n")

# Method 1: Temporary variable
a, b = x, y
temp = a
a = b
b = temp
print(f"Method 1 (Temp var):   a={a}, b={b}")

# Method 2: Tuple unpacking
a, b = x, y
a, b = b, a
print(f"Method 2 (Tuple):      a={a}, b={b}")

# Method 3: Addition/Subtraction
a, b = x, y
a = a + b
b = a - b
a = a - b
print(f"Method 3 (Add/Sub):    a={a}, b={b}")

# Method 4: Multiplication/Division
a, b = x, y
a = a * b
b = a // b
a = a // b
print(f"Method 4 (Mul/Div):    a={a}, b={b}")

# Method 5: XOR bitwise
a, b = x, y
a = a ^ b
b = a ^ b
a = a ^ b
print(f"Method 5 (XOR):        a={a}, b={b}")

# Method 6: List
a, b = x, y
arr = [a, b]
a = arr[1]
b = arr[0]
print(f"Method 6 (List):       a={a}, b={b}")

print("\n All methods produce same result!")

View the complete Io programs page.

Program Console Method 10: Comparison of all swap methods Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.