Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk1 min

How to Swap Two Numbers in Python (With and Without a Function)

Python’s `a, b = b, a` idiom swaps two values directly. Learn the temporary-variable method, how to return a swapped pair from a function, and when assignment order matters.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To swap two values in Python, use a, b = b, a. Python evaluates the values on the right before unpacking them into the names on the left, so the values exchange places without a temporary variable.

Swap two values with one assignment

a = 10
b = 20

a, b = b, a

print(a)  # 20
print(b)  # 10

The comma is the key: b, a forms a tuple of the current values, and Python unpacks that pair into a and b. Parentheses are optional, so a, b = b, a is the usual concise idiom. The Python 3.14.8 Language Reference itself gives this assignment as an example of swapping two variables: Simple statements: assignment statements.

Swap values with a temporary variable

a = 10
b = 20

temp = a
a = b
b = temp

This three-step version first saves the original value of a, then replaces a with b, and finally puts the saved value into b. It is useful for understanding the underlying idea of preserving a value before overwriting its name, but it is not required for an ordinary two-variable swap in Python.

Swap values through a function

def swapped(a, b):
    return b, a

x = 10
y = 20
x, y = swapped(x, y)

The function returns the two values in reverse order as a pair. The caller must unpack that returned pair—or assign it to another name—to rebind its own variables. Assigning to the function parameters alone does not reassign the caller’s local names; parameter names are local to the function call.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When swapping indexed items, mind assignment order

For simple names such as a and b, the direct idiom is straightforward. Assignments with overlapping targets, such as an index variable and an item addressed by that variable, are more subtle: Python assigns targets on the left from left to right. For example, in i, x[i] = 1, 2, i is changed before Python evaluates the target x[i], so that item target uses the new index. The language reference describes this left-to-right behavior and warns that overlapping targets can cause confusion: Python Language Reference, assignment statements.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.