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
BeginnerRead 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__")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for Dart Developers
1From Dart to Python
Run Your First Python ProgramInterpolation and printvar, final, and Python NamesFloor Versus TruncationBraces and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
for-in and rangeentries and enumerateLoop Updates and continuewhere, map, and List BuildingRecap: Build a Ranking2Values and Decisions
Null Safety and NoneList Equality ChangesConditions and Null DefaultsTernaries and elifRecap: Eligibility Rules3Collections and References
Lists and Negative IndexesRecords and Tuple UnpackingMap Lookups and KeyErrorSets and Shared ObjectsRecap: Clean an Inventory6Reliable Python Programs
stdin and Exact Formattingon, catch, finally, and withLibraries and Import ExecutionStatic Types Versus HintsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler