tuple

Store ordered items, including duplicates, without adding, removing or replacing them after creation. A nested list or dictionary can still change. In conditions, an empty tuple is false and a non-empty tuple is true.

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 copy items from a source you can loop through into a tuple. Nested collections remain shared with the source.

Syntax

tuple()
tuple(iterable, /)

Parameters

NameDescription
iterableIterable
Optional source you can loop through, such as a string, list or generator.
Depends on overload · Positional only
Default: ()

Returns

tuple — A new read-only tuple.

Example

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

tuple.literal

Create a tuple with comma-separated items. Use (value,) for one item; (value) alone is not a tuple.

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 position: 0 is first, -1 is last, and other negative indexes count from the end.

Syntax

tuple[index]

Parameters

NameDescription
tupletuple
Source tuple.
Required · Positional or named
No default
indexInt
Item position: 0 is first, -1 is last.
Required · Positional only
No default

Returns

Any? — The item at that position. Raises IndexError when the position is outside the tuple.

Example

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

tuple.slice

Return a new tuple from start up to, but not including, end. Negative indexes count from the end. Omitted bounds select the whole tuple in the step direction; nested collections remain shared.

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

Assign tuple items to variables from left to right. The number of variables must match the number of items or an error is raised. Groups of variables can be nested; *rest is not supported.

Syntax

x, y = tuple

Parameters

NameDescription
x, yTargets
Variables receiving the items; their count must match the tuple length.
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

Return the first matching position within the selected bounds, or raise ValueError if the value is 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
First position to search, starting at 0. Negative values count from the end; out-of-range bounds are limited to the available range.
Optional · Positional only
Default: 0
stopInt
Stop searching before this position. Negative values count from the end; out-of-range bounds are limited to the available range.
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))