Menu
CoddyTech

Tuples and Unpacking

Lesson 13 of 48 in Coddy's Python for C# Developers course.

Pack, unpack, and rearrange values with Python tuples.

C# reference excerpt

Executable statements use C# top-level program style; type-only examples are declarations. System names assume the usual using System directive.

var point = (X: 2, Y: 5);
var (x, y) = point;
Console.WriteLine($"{y} {x}");

Python

point = (2, 5)
x, y = point
print(y, x)

What changes

Python unpacking distributes iterable elements across names; the lengths must match unless a starred target is used. A tuple is immutable at the element-binding level, unlike a mutable C# ValueTuple field. A one-item tuple needs a comma: (2,). x, y = y, x evaluates the right-hand values before rebinding the left-hand names.

Transfer mistake

Tuple immutability does not freeze nested objects. A list stored in a tuple can still be changed. A Python tuple is not a copied C# value-type struct.

Your task

Two lines provide horizontal and vertical integers in [-100, 100]. The scaffold packs them into point. Unpack the tuple, swap coordinate order, and output the original tuple followed by the swapped tuple. Use Python tuple printing, one tuple per line. Keep the original tuple unchanged.

challenge icon

Challenge

Beginner

Two lines provide horizontal and vertical integers in [-100, 100]. The scaffold packs them into point. Unpack the tuple, swap coordinate order, and output the original tuple followed by the swapped tuple. Use Python tuple printing, one tuple per line. Keep the original tuple unchanged.

Editable implementation files: main.py. The locked driver.py is the supplied launcher or input/output harness and must remain unchanged.

Use the supplied input contract without prompts. Print only the required lines, each ending in a newline.

Try it yourself

# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module

run_module("main", run_name="__main__")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for C# Developers

Practice on your own: Online Python compiler