Menu
CoddyTech

Records and Tuple Unpacking

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

Dart records group positional or named fields and are immutable containers; modern Dart patterns can destructure them. Python tuples group positional values and unpack through assignment. A one-element tuple needs a trailing comma. Tuple immutability is shallow: a list held in a tuple can still mutate, just as immutable record fields can refer to mutable objects. Python unpacking without a starred target requires an exact element count. Parallel assignment evaluates the right-hand side before rebinding targets, enabling a swap. This course uses Python tuples directly and does not require Dart record syntax in executable source examples.

Dart

void main() {
  var x = 3;
  var y = 8;
  final oldX = x;
  x = y;
  y = oldX;
  print('$x $y');
}

Python

try:
    x, y = [3]
except ValueError:
    print('unpack error')
x, y = 3, 8
x, y = y, x
print(x, y)

Porting trap

Do not confuse a tuple's fixed slots with deep immutability. Referenced mutable state can still change.

challenge icon

Challenge

Beginner

Read an integer horizontal coordinate and an integer vertical coordinate on two lines. The scaffold packs them into a tuple named point. Unpack the coordinates, swap their order, then print the original tuple followed by the swapped tuple on separate lines. Values are from -100 to 100; use Python tuple format.

Print only the requested output, with a newline after each line. Keep the supplied input scaffold. No loops or functions are needed.

Your code goes in main.py. The locked driver.py starts your program and cannot be edited.

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 Dart Developers

Practice on your own: Online Python compiler