thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

05.02 · Swap values and compare sets / Practice · 11 min

Complete the three original swaps

Each pair must retain both original values, just under the opposite names.

Activity 2 of 6 in Swap values and compare sets

All courses

Python 3: Lists / 05.02

Practice value swapping

Not started

Exchange the source numeric and name values while keeping each technique visible.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Rob swaps John and Doe between first and last name holders.
Rob’s comic strip · Swap values and compare sets

Your task: three methods

Keep x=100 and y=200; exchange them using a temporary variable. Print x, then y.

Keep m=8 and n=12; use the source’s addition/subtraction sequence to swap their values. Print m, then n.

Keep first_name="John" and last_name="Doe"; exchange them using paired assignment. Print first_name, then last_name. Expect six lines: 200, 100, 12, 8, Doe, John.

Trace the intermediate values

The checks inspect final named values and output, not which swapping technique produced them. Review the requested method in each section. After passing, try equal values and a negative numeric value. Restore the original inputs for the checks.

Build it one step at a time

Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

4 steps · Saved work · Instant feedback

YOUR TURN

Swap three pairs without losing values

Use a temporary name for x/y, arithmetic for m/n and paired assignment for first_name/last_name. Print each pair after its swap in the requested order.

Expected output
200
100
12
8
Doe
John

Keep the starting inputs. Print these lines in this order, without extra output.

  • Store the requested x
  • Store the requested y
  • Store the requested m
  • Store the requested n
  • Store the requested first name
  • Store the requested last name
  • Output 1 matches the requested result
  • Output 2 matches the requested result
  • Output 3 matches the requested result
  • Output 4 matches the requested result
  • Output 5 matches the requested result
  • Output 6 matches the requested result

Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Need a nudge?

Preserve the original before replacing it

Save the original x in temporary, then assign y to x and temporary to y.

The right side is evaluated first

first_name, last_name = last_name, first_name uses both original values before assigning the two targets.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

Keep both originals available. If both names end with the same old value, an assignment happened too early.