# Tuples

Tuples are ordered and immutable collections. They are similar to lists, but you cannot change their contents after creation.

## Creating Tuples

```python
empty = ()
numbers = (1, 2, 3)
mixed = (1, "a", 2.5)

packed = "cat", "dog", "rabbit"
cat, dog, rabbit = packed
```

Single-item tuples need a trailing comma:

```python
single = (1,)
```

## Accessing Items

```python
t = (10, 20, 30, 40)

print(t[0])   # 10
print(t[-1])  # 40
print(t[1:3]) # (20, 30)
```

## Immutability

```python
t = (1, 2, 3)
# t[0] = 9  # TypeError
```

If a tuple contains a mutable object (like a list), that inner object can be changed:

```python
t = (1, [2, 3])
t[1][0] = 99
print(t)  # (1, [99, 3])
```

## Common Methods

```python
t = ("a", "p", "p", "l", "e")
print(t.count("p"))  # 2
print(t.index("l"))  # 3
```

## Membership and Iteration

```python
t = (1, 2, 3)
print(2 in t)  # True

for item in t:
    print(item)
```

## When to Use Tuples

* Fixed collections that should not change.
* Keys in dictionaries (tuples are hashable if they contain only immutable items).

[Next](https://kallyasmedia.gitbook.io/cit-python-cohort-three/week2/data-types/sets) | [Previous](https://kallyasmedia.gitbook.io/cit-python-cohort-three/week2/data-types/python_lists)


---

# 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://kallyasmedia.gitbook.io/cit-python-cohort-three/week2/data-types/python_tuples.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.
