Skip to content
Suggested Lab. Exercises · Q6

Q.Write a program that has a user defined function to accept 2 numbers as parameters, if number 1 is less than number 2 then numbers are swapped and returned, i.e., number 2 is returned in place of number1 and number 1 is reformed in place of number 2, otherwise the same order is returned.

Odisha ChseTextbookSubjective· 3mImportance★★★★★est
95% · 38/40 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

A user-defined function with two parameters swaps them with tuple assignment num1, num2 = num2, num1 only when num1 < num2, and returns both values (a tuple) so the caller receives them in the possibly-swapped order.

The idea. Python functions can return more than one value — the values travel back as a tuple and can be unpacked directly into two variables. Combined with Python's elegant one-line swap (x, y = y, x, no temporary variable needed), the task becomes tiny.

def arrange(num1, num2):
    """Return (num2, num1) if num1 < num2, else (num1, num2)."""
    if num1 < num2:
        num1, num2 = num2, num1   # swap without a temporary variable
    return num1, num2

a = int(input("Enter number 1: "))
b = int(input("Enter number 2: "))
a, b = arrange(a, b)
print("Number 1 =", a)
print("Number 2 =", b)

Key lines

  • num1, num2 = num2, num1 — the right side is evaluated first as a tuple (num2, num1), then unpacked; that is why no third variable is needed.
  • return num1, num2 — returns the tuple (num1, num2); the caller unpacks it with a, b = arrange(a, b).

Expected output

Run 1 (swap happens):

Enter number 1: 5
Enter number 2: 9
Number 1 = 9 …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.