> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Tuples

> This article explains Python tuples, their immutability, creation, usage, and comparison with lists.

Python offers two primary sequence data types: lists and tuples. While lists are mutable—allowing you to modify their data by deleting items or appending new ones—tuples are immutable, meaning their data remains constant once created.

<Callout icon="lightbulb" color="#1CB2FE">
  Remember: Unlike lists, tuples cannot be modified after creation. This property makes them useful in scenarios where a constant set of values is required.
</Callout>

## Creating and Using Tuples

A tuple is defined using parentheses or simply comma-separated values. Below is an example of creating and printing a tuple:

```python theme={null}
>>> tuple1 = (1, 2, 3)
>>> print(tuple1)
(1, 2, 3)
```

Much like lists, tuples support data access through loops and slicing. For instance, you can iterate through a tuple using a for loop:

```python theme={null}
>>> tuple1 = (1, 2, 3)
>>> for item in tuple1:
...     print(item)
```

Attempting to modify a tuple, however, results in errors. Unlike lists, tuples do not support methods like `append`, nor can you assign a new value to an individual element:

```python theme={null}
>>> tuple1 = (1, 2, 3)
>>> tuple1.append(4)
AttributeError: 'tuple' object has no attribute 'append'

>>> tuple1[1] = 9
TypeError: 'tuple' object does not support item assignment
```

## Tuple Characteristics

* **Immutable Structure:** Once a tuple is created, its contents cannot be changed.
* **Versatile Storage:** Tuples can store elements of different data types including integers, strings, variables, and even other tuples.

For example, you can combine multiple data types into a single tuple:

```python theme={null}
>>> age = 22
>>> tuple1 = (1, "Lydia", age, (1, 2))
```

### Single-Element Tuples

Creating a tuple with a single element requires a trailing comma to distinguish it from a regular parenthesized expression:

```python theme={null}
>>> tuple1 = (1,)
>>> tuple2 = 1,
```

## Comparing Lists and Tuples

While lists offer flexibility through mutability, tuples provide a reliable structure for fixed data. A quick comparison:

| Data Type | Mutability | Creation Syntax     | Use Case                          |
| --------- | ---------- | ------------------- | --------------------------------- |
| List      | Mutable    | Square brackets \[] | Data that may change over time    |
| Tuple     | Immutable  | Parentheses ()      | Fixed data that should not change |

That’s it for now! Dive into some hands-on practice with tuples in this lesson to reinforce your understanding and explore their practical applications.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/python-basics/module/999967d9-7cca-48f8-893c-5e61014671f8/lesson/b8cb75b0-f340-45ec-ae6c-a188272e9f3f" />
</CardGroup>
