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

# Python Tuples

## 1. **What is a Tuple?**

* A **tuple** is an ordered, immutable (unchangeable) collection of elements.
* Tuples are defined using parentheses `()` or the `tuple()` constructor.
* Tuples can contain elements of different data types.

**Example**:

```python theme={"system"}
person = ("Raj", 25, "Chennai")
```

## 2. **Tuple Operations**

### **1. Accessing Elements**

* Use indexing to access individual elements in a tuple.
* Python uses zero-based indexing (the first element has index `0`).

**Example**:

```python theme={"system"}
person = ("Ram", 30, "Bangalore")
print(person[0])  # Output: Ram
print(person[-1])  # Output: Bangalore (negative indexing starts from the end)
```

### **2. Slicing**

* Extract a subtuple using a range of indices.
* Syntax: `tuple[start:stop:step]`

**Example**:

```python theme={"system"}
numbers = (1, 2, 3, 4, 5)
print(numbers[1:4])  # Output: (2, 3, 4) (from index 1 to 3)
print(numbers[::2])  # Output: (1, 3, 5) (every second element)
```

### **3. Length**

* Use `len()` to find the number of elements in a tuple.

**Example**:

```python theme={"system"}
fruits = ("apple", "banana", "cherry")
print(len(fruits))  # Output: 3
```

### **4. Concatenation**

* Combine two or more tuples using the `+` operator.

**Example**:

```python theme={"system"}
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
combined = tuple1 + tuple2
print(combined)  # Output: (1, 2, 3, 4, 5, 6)
```

### **5. Repetition**

* Repeat a tuple using the `*` operator.

**Example**:

```python theme={"system"}
numbers = (1, 2)
repeated = numbers * 3
print(repeated)  # Output: (1, 2, 1, 2, 1, 2)
```

### **6. Checking for Elements**

* Use the `in` keyword to check if an element exists in a tuple.

**Example**:

```python theme={"system"}
fruits = ("apple", "banana", "cherry")
print("banana" in fruits)  # Output: True
print("mango" in fruits)   # Output: False
```

## 3. **Tuple Methods**

### **1. `count()`**

* Returns the number of times a specified value occurs in a tuple.

**Example**:

```python theme={"system"}
numbers = (1, 2, 3, 2, 4, 2)
print(numbers.count(2))  # Output: 3
```

### **2. `index()`**

* Returns the index of the first occurrence of a specified value.

**Example**:

```python theme={"system"}
fruits = ("apple", "banana", "cherry", "banana")
print(fruits.index("banana"))  # Output: 1
```

## 4. **Immutable Nature of Tuples**

* Tuples are immutable, meaning their elements cannot be changed after creation.
* However, if a tuple contains mutable objects (e.g., lists), those objects can be modified.

**Example**:

```python theme={"system"}
person = ("Anand", 28, ["Chennai", "Bangalore"])
person[2].append("Mumbai")  # Modify the list inside the tuple
print(person)  # Output: ('Anand', 28, ['Chennai', 'Bangalore', 'Mumbai'])
```

## 5. **Unpacking Tuples**

* Assign the elements of a tuple to variables in a single statement.

**Example**:

```python theme={"system"}
person = ("Bala", 35, "Delhi")
name, age, city = person
print(name)  # Output: Bala
print(age)   # Output: 35
print(city)  # Output: Delhi
```

## 6. **Nested Tuples**

* A tuple can contain other tuples, creating a multi-level structure.

**Example**:

```python theme={"system"}
matrix = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
print(matrix[1][2])  # Output: 6 (access element in the second row, third column)
```

## 7. **Additional Examples**

* **Creating a Tuple**:
  ```python theme={"system"}
  person = ("Karthik", 40, "Hyderabad")
  print(person)  # Output: ('Karthik', 40, 'Hyderabad')
  ```

* **Accessing Elements**:
  ```python theme={"system"}
  name = person[0]
  print(name)  # Output: Karthik
  ```

* **Slicing**:
  ```python theme={"system"}
  numbers = (10, 20, 30, 40, 50)
  print(numbers[1:4])  # Output: (20, 30, 40)
  ```

* **Unpacking**:
  ```python theme={"system"}
  name, age, city = person
  print(city)  # Output: Hyderabad
  ```

* **Checking for Elements**:
  ```python theme={"system"}
  fruits = ("apple", "banana", "cherry")
  print("banana" in fruits)  # Output: True
  ```

## 8. **Best Practices**

* Use tuples for collections of items that should not change (e.g., constants, configurations).
* Use parentheses for clarity, even when defining a single-element tuple (e.g., `(1,)`).
* Prefer tuples over lists for immutable data to ensure data integrity.
