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

# Python Collections

# 23. Python Lists

## Create List

```python theme={null}
fruits = ["apple", "banana", "mango"]

print(fruits)
```

Output

```text theme={null}
['apple', 'banana', 'mango']
```

Explanation

Lists store ordered, mutable collections.

***

## Access List Items

```python theme={null}
print(fruits[0])
```

Output

```text theme={null}
apple
```

Explanation

Access items using indexes.

***

## Change List Items

```python theme={null}
fruits[1] = "orange"

print(fruits)
```

Output

```text theme={null}
['apple', 'orange', 'mango']
```

Explanation

Modify items using indexes.

***

## Add List Items

```python theme={null}
fruits.append("grapes")

print(fruits)
```

Output

```text theme={null}
['apple', 'banana', 'mango', 'grapes']
```

Explanation

`append()` adds an item to the end.

***

## Remove List Items

```python theme={null}
fruits.remove("banana")

print(fruits)
```

Output

```text theme={null}
['apple', 'mango']
```

Explanation

`remove()` deletes a specified item.

***

## Loop Lists

```python theme={null}
for fruit in fruits:
    print(fruit)
```

Output

```text theme={null}
apple
banana
mango
```

Explanation

Iterate through each item in the list.

***

## List Comprehension

```python theme={null}
numbers = [x * 2 for x in range(5)]

print(numbers)
```

Output

```text theme={null}
[0, 2, 4, 6, 8]
```

Explanation

Create lists using a concise syntax.

***

## Sort Lists

```python theme={null}
numbers = [5, 2, 4]

numbers.sort()

print(numbers)
```

Output

```text theme={null}
[2, 4, 5]
```

Explanation

Sort items in ascending order.

***

## Copy Lists

```python theme={null}
list1 = [1, 2, 3]
list2 = list1.copy()

print(list2)
```

Output

```text theme={null}
[1, 2, 3]
```

Explanation

Create a separate copy of a list.

***

## Join Lists

```python theme={null}
a = [1, 2]
b = [3, 4]

print(a + b)
```

Output

```text theme={null}
[1, 2, 3, 4]
```

Explanation

Combine two lists.

***

## Common List Methods

| Method      | Purpose              |
| :---------- | :------------------- |
| `append()`  | Add item at end      |
| `insert()`  | Insert at position   |
| `extend()`  | Add another iterable |
| `remove()`  | Remove specific item |
| `pop()`     | Remove by index      |
| `clear()`   | Remove all items     |
| `copy()`    | Copy list            |
| `sort()`    | Sort list            |
| `reverse()` | Reverse list         |
| `count()`   | Count occurrences    |
| `index()`   | Find position        |

***

# 24. Python Tuples

## 24.1 Create Tuple

```python theme={null}
colors = ("red", "green", "blue")

print(colors)
```

### Output

```text theme={null}
('red', 'green', 'blue')
```

**Explanation:** A tuple stores multiple ordered items.

***

## 24.2 Tuple Length

```python theme={null}
colors = ("red", "green", "blue")

print(len(colors))
```

### Output

```text theme={null}
3
```

**Explanation:** `len()` returns the number of items in a tuple.

***

## 24.3 Create Tuple With One Item

```python theme={null}
fruit = ("apple",)

print(type(fruit))
```

### Output

```text theme={null}
<class 'tuple'>
```

**Explanation:** A single-item tuple must end with a comma.

***

## 24.4 Tuple Data Types

```python theme={null}
numbers = (1, 2, 3)
names = ("John", "Sam")
mixed = (1, "Python", True)

print(numbers)
print(names)
print(mixed)
```

### Output

```text theme={null}
(1, 2, 3)
('John', 'Sam')
(1, 'Python', True)
```

**Explanation:** Tuples can store different data types.

***

## 24.5 Access Tuple Items

```python theme={null}
colors = ("red", "green", "blue")

print(colors[1])
```

### Output

```text theme={null}
green
```

**Explanation:** Access tuple items using indexes.

***

## 24.6 Negative Indexing

```python theme={null}
colors = ("red", "green", "blue")

print(colors[-1])
```

### Output

```text theme={null}
blue
```

**Explanation:** Negative indexes start from the end.

***

## 24.7 Range of Indexes

```python theme={null}
colors = ("red", "green", "blue", "yellow")

print(colors[1:3])
```

### Output

```text theme={null}
('green', 'blue')
```

**Explanation:** Slice returns a portion of the tuple.

***

## 24.8 Check if Item Exists

```python theme={null}
colors = ("red", "green", "blue")

print("green" in colors)
```

### Output

```text theme={null}
True
```

**Explanation:** `in` checks whether an item exists.

***

## 24.9 Change Tuple Items

```python theme={null}
colors = ("red", "green", "blue")

temp = list(colors)
temp[1] = "black"
colors = tuple(temp)

print(colors)
```

### Output

```text theme={null}
('red', 'black', 'blue')
```

**Explanation:** Convert to a list to modify tuple items.

***

## 24.10 Add Items

```python theme={null}
colors = ("red", "green")

colors += ("blue",)

print(colors)
```

### Output

```text theme={null}
('red', 'green', 'blue')
```

**Explanation:** Tuples are immutable, so create a new tuple.

***

## 24.11 Remove Items

```python theme={null}
colors = ("red", "green", "blue")

temp = list(colors)
temp.remove("green")
colors = tuple(temp)

print(colors)
```

### Output

```text theme={null}
('red', 'blue')
```

**Explanation:** Convert to a list to remove items.

***

## 24.12 Unpack Tuples

```python theme={null}
colors = ("red", "green", "blue")

a, b, c = colors

print(a)
print(b)
print(c)
```

### Output

```text theme={null}
red
green
blue
```

**Explanation:** Assign tuple values to separate variables.

***

## 24.13 Using Asterisk (\*)

```python theme={null}
colors = ("red", "green", "blue", "yellow")

a, *b = colors

print(a)
print(b)
```

### Output

```text theme={null}
red
['green', 'blue', 'yellow']
```

**Explanation:** `*` collects remaining values into a list.

***

## 24.14 Loop Through Tuple

```python theme={null}
colors = ("red", "green", "blue")

for color in colors:
    print(color)
```

### Output

```text theme={null}
red
green
blue
```

**Explanation:** Loop through tuple items using `for`.

***

## 24.15 Join Tuples

```python theme={null}
tuple1 = (1, 2)
tuple2 = (3, 4)

print(tuple1 + tuple2)
```

### Output

```text theme={null}
(1, 2, 3, 4)
```

**Explanation:** `+` joins two tuples.

***

## 24.16 Multiply Tuples

```python theme={null}
numbers = (1, 2)

print(numbers * 3)
```

### Output

```text theme={null}
(1, 2, 1, 2, 1, 2)
```

**Explanation:** `*` repeats tuple elements.

***

## 24.17 Tuple Methods

### count()

```python theme={null}
numbers = (1, 2, 2, 3)

print(numbers.count(2))
```

Output

```text theme={null}
2
```

**Explanation:** Counts occurrences of an item.

***

### index()

```python theme={null}
numbers = (1, 2, 3)

print(numbers.index(2))
```

Output

```text theme={null}
1
```

**Explanation:** Returns the index of the first occurrence.

***

# 25. Python Sets

## 25.1 Create Set

```python theme={null}
fruits = {"apple", "banana", "mango"}

print(fruits)
```

### Output

```text theme={null}
{'apple', 'banana', 'mango'}
```

**Explanation:** Sets store unordered unique items.

***

## 25.2 Set Length

```python theme={null}
fruits = {"apple", "banana", "mango"}

print(len(fruits))
```

### Output

```text theme={null}
3
```

**Explanation:** Returns the number of items.

***

## 25.3 Duplicate Values

```python theme={null}
numbers = {1, 2, 2, 3}

print(numbers)
```

### Output

```text theme={null}
{1, 2, 3}
```

**Explanation:** Duplicate values are removed automatically.

***

## 25.4 Access Set Items

```python theme={null}
fruits = {"apple", "banana", "mango"}

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

### Output

```text theme={null}
apple
banana
mango
```

**Explanation:** Sets do not support indexing.

***

## 25.5 Check Item Exists

```python theme={null}
fruits = {"apple", "banana"}

print("banana" in fruits)
```

### Output

```text theme={null}
True
```

**Explanation:** `in` checks item existence.

***

## 25.6 Add Items

```python theme={null}
fruits = {"apple"}

fruits.add("banana")

print(fruits)
```

### Output

```text theme={null}
{'apple', 'banana'}
```

**Explanation:** `add()` inserts one item.

***

## 25.7 Add Multiple Items

```python theme={null}
fruits = {"apple"}

fruits.update(["banana", "mango"])

print(fruits)
```

### Output

```text theme={null}
{'apple', 'banana', 'mango'}
```

**Explanation:** `update()` adds multiple items.

***

## 25.8 Remove Item

```python theme={null}
fruits = {"apple", "banana"}

fruits.remove("banana")

print(fruits)
```

### Output

```text theme={null}
{'apple'}
```

**Explanation:** `remove()` deletes an item.

***

## 25.9 discard()

```python theme={null}
fruits = {"apple", "banana"}

fruits.discard("orange")

print(fruits)
```

### Output

```text theme={null}
{'apple', 'banana'}
```

**Explanation:** `discard()` doesn't raise an error if the item doesn't exist.

***

## 25.10 pop()

```python theme={null}
fruits = {"apple", "banana"}

fruits.pop()

print(fruits)
```

### Output

```text theme={null}
{'banana'}
```

**Explanation:** Removes a random item.

***

## 25.11 clear()

```python theme={null}
fruits = {"apple", "banana"}

fruits.clear()

print(fruits)
```

### Output

```text theme={null}
set()
```

**Explanation:** Removes all items.

***

## 25.12 Loop Set

```python theme={null}
fruits = {"apple", "banana"}

for fruit in fruits:
    print(fruit)
```

### Output

```text theme={null}
apple
banana
```

**Explanation:** Iterate through every item.

***

## 25.13 Join Sets (Union)

```python theme={null}
a = {1, 2}
b = {3, 4}

print(a.union(b))
```

### Output

```text theme={null}
{1, 2, 3, 4}
```

**Explanation:** Combines all unique elements.

***

## 25.14 Intersection

```python theme={null}
a = {1, 2, 3}
b = {2, 3, 4}

print(a.intersection(b))
```

### Output

```text theme={null}
{2, 3}
```

**Explanation:** Returns common items.

***

## 25.15 Difference

```python theme={null}
a = {1, 2, 3}
b = {2, 3}

print(a.difference(b))
```

### Output

```text theme={null}
{1}
```

**Explanation:** Returns items only in the first set.

***

## Common Set Methods

| Method         | Purpose            |
| :------------- | :----------------- |
| add()          | Add one item       |
| update()       | Add multiple items |
| remove()       | Remove item        |
| discard()      | Remove safely      |
| pop()          | Remove random item |
| clear()        | Remove all items   |
| union()        | Join sets          |
| intersection() | Common elements    |
| difference()   | Different elements |
| copy()         | Copy set           |

***

# 26. Python Dictionaries

## 26.1 Create Dictionary

```python theme={null}
student = {
    "name": "John",
    "age": 20
}

print(student)
```

### Output

```text theme={null}
{'name': 'John', 'age': 20}
```

**Explanation:** Dictionaries store data as key-value pairs.

***

## 26.2 Dictionary Length

```python theme={null}
student = {
    "name": "John",
    "age": 20
}

print(len(student))
```

### Output

```text theme={null}
2
```

**Explanation:** Returns the number of key-value pairs.

***

## 26.3 Access Items

```python theme={null}
student = {
    "name": "John",
    "age": 20
}

print(student["name"])
```

### Output

```text theme={null}
John
```

**Explanation:** Access values using keys.

***

## 26.4 get()

```python theme={null}
student = {
    "name": "John"
}

print(student.get("name"))
```

### Output

```text theme={null}
John
```

**Explanation:** `get()` safely returns a value.

***

## 26.5 keys()

```python theme={null}
student = {
    "name": "John",
    "age": 20
}

print(student.keys())
```

### Output

```text theme={null}
dict_keys(['name', 'age'])
```

**Explanation:** Returns all keys.

***

## 26.6 values()

```python theme={null}
student = {
    "name": "John",
    "age": 20
}

print(student.values())
```

### Output

```text theme={null}
dict_values(['John', 20])
```

**Explanation:** Returns all values.

***

## 26.7 items()

```python theme={null}
student = {
    "name": "John",
    "age": 20
}

print(student.items())
```

### Output

```text theme={null}
dict_items([('name', 'John'), ('age', 20)])
```

**Explanation:** Returns key-value pairs.

***

## 26.8 Change Items

```python theme={null}
student = {
    "name": "John",
    "age": 20
}

student["age"] = 21

print(student)
```

### Output

```text theme={null}
{'name': 'John', 'age': 21}
```

**Explanation:** Change a value using its key.

***

## 26.9 update()

```python theme={null}
student = {
    "name": "John"
}

student.update({"age": 20})

print(student)
```

### Output

```text theme={null}
{'name': 'John', 'age': 20}
```

**Explanation:** Adds or updates key-value pairs.

***

## 26.10 Add Items

```python theme={null}
student = {
    "name": "John"
}

student["city"] = "Chennai"

print(student)
```

### Output

```text theme={null}
{'name': 'John', 'city': 'Chennai'}
```

**Explanation:** Assign a new key to add an item.

***

## 26.11 Remove Items

### pop()

```python theme={null}
student = {
    "name": "John",
    "age": 20
}

student.pop("age")

print(student)
```

Output

```text theme={null}
{'name': 'John'}
```

**Explanation:** Removes a specified key.

***

### popitem()

```python theme={null}
student = {
    "name": "John",
    "age": 20
}

student.popitem()

print(student)
```

### Output

```text theme={null}
{'name': 'John'}
```

**Explanation:** Removes the last inserted item.

***

### del

```python theme={null}
student = {
    "name": "John",
    "age": 20
}

del student["age"]

print(student)
```

### Output

```text theme={null}
{'name': 'John'}
```

**Explanation:** Deletes a key-value pair.

***

### clear()

```python theme={null}
student = {
    "name": "John",
    "age": 20
}

student.clear()

print(student)
```

### Output

```text theme={null}
{}
```

**Explanation:** Removes all items.

***

## 26.12 Loop Dictionaries

```python theme={null}
student = {
    "name": "John",
    "age": 20
}

for key, value in student.items():
    print(key, value)
```

### Output

```text theme={null}
name John
age 20
```

**Explanation:** Iterate through keys and values.

***

## 26.13 Copy Dictionary

```python theme={null}
student = {
    "name": "John"
}

copy_dict = student.copy()

print(copy_dict)
```

### Output

```text theme={null}
{'name': 'John'}
```

**Explanation:** Creates a separate copy of the dictionary.

***

## 26.14 Nested Dictionaries

```python theme={null}
students = {
    "student1": {
        "name": "John",
        "age": 20
    },
    "student2": {
        "name": "Alice",
        "age": 21
    }
}

print(students["student1"]["name"])
```

### Output

```text theme={null}
John
```

**Explanation:** A dictionary can contain other dictionaries.

***

## Common Dictionary Methods

| Method       | Purpose                        |
| :----------- | :----------------------------- |
| get()        | Get value safely               |
| keys()       | Return all keys                |
| values()     | Return all values              |
| items()      | Return key-value pairs         |
| update()     | Add/update items               |
| pop()        | Remove by key                  |
| popitem()    | Remove last item               |
| clear()      | Remove all items               |
| copy()       | Copy dictionary                |
| setdefault() | Return value or insert default |
