Menu
CoddyTech

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

Practice on your own: Online Python compiler