Arrays, Vectors, and Tuples
Lesson 13 of 48 in Coddy's Python for GDScript Developers course.
Godot offers Arrays and engine vector types; Python tuples group positional values without inheriting Vector2 operations. Unpacking binds iterable elements to names. A tuple's slots cannot be assigned, although a referenced mutable element can change. The comma forms a tuple, including a trailing comma for a single element. Python simultaneous assignment can swap names after evaluating the whole right side.
GDScript
extends SceneTree
func _init():
var point = Vector2i(3, 8)
prints(point.y, point.x)
quit()Python
point = (3, 8)
x, y = point
x, y = y, x
print(x, y)Porting trap
Do not expect a two-item tuple to support Godot vector arithmetic. Choose the representation for the task: these coordinates are positional values, not scene vectors.
Complete the task in its editable modules. The locked driver.py supplies execution; keep the required API names.
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 GDScript Developers
1From GDScript to Python
Run Your First Python ProgramPrint Is Not printsVar, Const, and BindingsInteger Division DifferencesSimilar Indentation, New ScopeRecap: Rewrite a Receipt4Iteration and Transformations
Ranges and RebindingItems and enumerateWhile and Early ExitArray Callbacks and KeysRecap: Build a Ranking2Values and Decisions
Null and NoneThe is Keyword Changes MeaningLogical Results and OperandsConditional ExpressionsRecap: Eligibility Rules3Collections and Shared State
Array and List APIsArrays, Vectors, and TuplesDictionary Lookup RulesDuplicate and Shallow CopyRecap: Clean an Inventory6Input, Errors, and Modules
Console Work Outside GodotValidation and ExceptionsScript Resources and ImportsTyped GDScript and AnnotationsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler