# Touple

```python
"""
Tuples are an ordered collection of values that cannot be modified at
runtime. This module shows how tuples are created, iterated, accessed
and combined.
"""


def main():
    # This is a tuple of integers
    immutable = (1, 2, 3, 4)

    # It can be indexed like a list
    assert immutable[0] == 1
    assert immutable[-1] == 4

    # It can be sliced like a list
    assert immutable[1:3] == (2, 3)
    assert immutable[3:4] == (4,)
    assert immutable[1::2] == (2, 4)
    assert immutable[::-1] == (4, 3, 2, 1)

    # It can be iterated over like a list
    for ix, number in enumerate(immutable):
        assert immutable[ix] == number

    # But its contents cannot be changed. As an alternative, we can
    # create new tuples from existing tuples
    bigger_immutable = immutable + (5, 6)
    assert bigger_immutable == (1, 2, 3, 4, 5, 6)
    smaller_immutable = immutable[0:2]
    assert smaller_immutable == (1, 2)

    # We use tuples when the number of items is consistent. An example
    # where this can help is a 2D game with X and Y coordinates. Using a
    # tuple with two numbers can ensure that the number of coordinates
    # doesn't change to one, three, four, etc.
    moved_count = 0
    pos_x, pos_y = (0, 0)
    for i in range(1, 5, 2):
        moved_count += 1
        pos_x, pos_y = (pos_x + 10 * i, pos_y + 15 * i)
    assert moved_count == 2
    assert pos_x == 40 and pos_y == 60


if __name__ == "__main__":
    main()

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://py-v2.gitbook.io/datastructures-in-pytho/aux-exploration/subject/touple.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
