tuple

An ordered read-only collection. Use tuple literals when values should stay fixed after creation or when returning grouped values for destructuring.

Syntax

tuple(iterable=(), /)
(value1, value2, ...)

Returns

Example

offset = (0, 1)
x = offset[0]
y = offset[1]
if 1 in offset:
    text("offset ready")

Methods

9 methods

tuple.constructor

Create an empty tuple or clone another iterable as tuple. Use a tuple literal for multiple inline values.

Syntax

tuple()
tuple(iterable, /)

Parameters

NameDescription
iterableIterable
Optional iterable to clone into a tuple.
Depends on overload · Positional only
Default: None

Returns

tuple — A new read-only tuple.

Example

coords = (120, 340)
copy = tuple(coords)

tuple.literal

Create a tuple inline using parentheses and commas.

Syntax

(value1, value2, ...)

Parameters

NameDescription
valueAny?
Each item written inside tuple parentheses.
Required · Positional or named
No default

Returns

tuple — A new tuple preserving literal order.

Example

offset = (0, 1)
click(Point(500, 900), offset=offset)

tuple.length

Count how many items are in the tuple.

Syntax

len(tuple, /)

Parameters

NameDescription
tupletuple
The tuple to count.
Required · Positional only
No default

Returns

Int — Number of tuple items.

Example

offset = (0, 1)
count = len(offset)      # returns 2

tuple.index_access

Read one tuple item by index. Negative indexes count from the end.

Syntax

tuple[index]

Parameters

NameDescription
tupletuple
Source tuple.
Required · Positional or named
No default
indexInt
Item index. Use -1 for the last item.
Required · Positional only
No default

Returns

Any? — tuple item at index, or None when out of range.

Example

pair = (10, 20)
x = pair[0]              # returns 10
y = pair[-1]             # returns 20

tuple.slice

Copy part of a tuple using Python-style slicing. Omitted bounds use the start or end; negative indexes count from the end.

Syntax

tuple[start:end[:step]]

Parameters

NameDescription
startInt
First item index to include.
Optional · Positional only
Default: start of tuple
endInt
Stop before this item index.
Optional · Positional only
Default: end of tuple
stepInt
Distance between items. Cannot be 0; negative values read backward.
Optional · Positional only
Default: 1

Returns

tuple — A new tuple slice, clamped to the available range.

Example

values = (0, 1, 2, 3, 4)
middle = values[1:4]     # returns (1, 2, 3)
reverse = values[::-1]   # returns (4, 3, 2, 1, 0)

tuple.destructuring_assignment

Unpack tuple items into matching assignment targets. Nested targets are supported, but starred targets such as *rest are not supported.

Syntax

x, y = tuple

Parameters

NameDescription
x, yTargets
Assignment targets matching the tuple item count.
Required · Positional or named
No default
tupletuple
Source tuple to unpack.
Required · Positional only
No default

Returns

None — No direct return value. Targets receive tuple items left to right.

Example

label, (x, y) = ("target", (500, 900))

tuple.index_method

Find the first matching item index and fail visibly when absent.

Syntax

tuple.index(value, start=0, stop=len(tuple), /)

Parameters

NameDescription
valueAny?
Value that must exist in the tuple.
Required · Positional only
No default
startInt
Start index for the search.
Optional · Positional only
Default: 0
stopInt
Stop index for the search.
Optional · Positional only
Default: len(tuple)

Returns

Int — First matching item index.

Example

pair = ("farm", "pvp")
index = pair.index("pvp")

tuple.count_method

Count matching tuple items.

Syntax

tuple.count(value, /)

Parameters

NameDescription
valueAny?
Value to count.
Required · Positional only
No default

Returns

Int — Number of matches.

Example

pair = ("farm", "farm", "pvp")
count = pair.count("farm")

tuple.for_loop

Iterate tuple items from left to right.

Syntax

for item in tuple:

Parameters

NameDescription
itemAny
Current tuple item in loop.
Required · Positional or named
No default
tupletuple
tuple to iterate.
Required · Positional or named
No default

Returns

None — No direct return value.

Example

offset = (0, 1)
for item in offset:
    text(str(item))