|
| 1 | +--- |
| 2 | +id: python-tuple |
| 3 | +title: Tuple in Python |
| 4 | +sidebar_label: Tuple in Python #displays in sidebar |
| 5 | +sidebar_position: 9 |
| 6 | +tags: |
| 7 | + [ |
| 8 | + Python, |
| 9 | + List in Python, |
| 10 | + Introduction of python, |
| 11 | + Python Syntax, |
| 12 | + Variables, |
| 13 | + Operators, |
| 14 | + Type Casting, |
| 15 | + String, |
| 16 | + Tuple in Python |
| 17 | + ] |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | + |
| 22 | +# Tuples in Python |
| 23 | + |
| 24 | +A **Tuple** is an immutable, ordered collection of elements. |
| 25 | +Unlike lists, **tuples cannot be changed after creation**, which makes them useful for storing fixed collections of data. |
| 26 | + |
| 27 | + |
| 28 | +## Creating a Tuple |
| 29 | + |
| 30 | +Tuples are created using parentheses `()` or simply commas: |
| 31 | + |
| 32 | +```python |
| 33 | +# Empty Tuple |
| 34 | +empty_tuple = () |
| 35 | + |
| 36 | +# Tuple with multiple items |
| 37 | +fruits = ("apple", "banana", "cherry") |
| 38 | + |
| 39 | +# Tuple without parentheses (comma-separated) |
| 40 | +numbers = 1, 2, 3 |
| 41 | + |
| 42 | +# Single-element Tuple (Note the comma!) |
| 43 | +single = ("hello",) |
| 44 | +```` |
| 45 | + |
| 46 | +**Important:** Without the comma, Python does not recognize it as a tuple: |
| 47 | + |
| 48 | +```python |
| 49 | +not_a_tuple = ("hello") # This is a string, NOT a tuple |
| 50 | +``` |
| 51 | + |
| 52 | + |
| 53 | +## Accessing Elements |
| 54 | + |
| 55 | +Use indexing just like lists: |
| 56 | + |
| 57 | +```python |
| 58 | +fruits = ("apple", "banana", "cherry") |
| 59 | + |
| 60 | +print(fruits[0]) # apple |
| 61 | +print(fruits[1]) # banana |
| 62 | +print(fruits[-1]) # cherry |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | +## Slicing Tuples |
| 67 | + |
| 68 | +Tuples support slicing operations: |
| 69 | + |
| 70 | +```python |
| 71 | +numbers = (10, 20, 30, 40, 50) |
| 72 | + |
| 73 | +print(numbers[1:4]) # (20, 30, 40) |
| 74 | +print(numbers[:3]) # (10, 20, 30) |
| 75 | +print(numbers[2:]) # (30, 40, 50) |
| 76 | +``` |
| 77 | + |
| 78 | + |
| 79 | +## Tuple Immutability |
| 80 | + |
| 81 | +Tuples cannot be modified after creation: |
| 82 | + |
| 83 | +```python |
| 84 | +fruits = ("apple", "banana", "cherry") |
| 85 | + |
| 86 | +# This will raise an error: |
| 87 | +fruits[1] = "mango" |
| 88 | +``` |
| 89 | + |
| 90 | +**Output:** |
| 91 | + |
| 92 | +``` |
| 93 | +TypeError: 'tuple' object does not support item assignment |
| 94 | +``` |
| 95 | + |
| 96 | +This property makes tuples **safe for constant data**, like coordinates, fixed configurations, etc. |
| 97 | + |
| 98 | + |
| 99 | +## Tuple Methods |
| 100 | + |
| 101 | +Tuples have only **two built-in methods**: |
| 102 | + |
| 103 | +| Method | Description | |
| 104 | +| ---------- | ------------------------------------------------ | |
| 105 | +| `count(x)` | Counts how many times `x` occurs in the tuple | |
| 106 | +| `index(x)` | Returns the index of the first occurrence of `x` | |
| 107 | + |
| 108 | +### Example |
| 109 | + |
| 110 | +```python |
| 111 | +numbers = (1, 2, 3, 2, 2, 4) |
| 112 | + |
| 113 | +print(numbers.count(2)) # 3 |
| 114 | +print(numbers.index(3)) # 2 |
| 115 | +``` |
| 116 | + |
| 117 | + |
| 118 | +## Tuple Packing and Unpacking |
| 119 | + |
| 120 | +**Packing:** Combining values into a tuple: |
| 121 | + |
| 122 | +```python |
| 123 | +data = "John", 25, "Engineer" |
| 124 | +print(data) # ('John', 25, 'Engineer') |
| 125 | +``` |
| 126 | + |
| 127 | +**Unpacking:** Assigning tuple elements to variables: |
| 128 | + |
| 129 | +```python |
| 130 | +name, age, profession = data |
| 131 | + |
| 132 | +print(name) # John |
| 133 | +print(age) # 25 |
| 134 | +print(profession) # Engineer |
| 135 | +``` |
| 136 | + |
| 137 | + |
| 138 | +## Nested Tuples |
| 139 | + |
| 140 | +Tuples can contain other tuples or collections: |
| 141 | + |
| 142 | +```python |
| 143 | +nested = ( |
| 144 | + (1, 2, 3), |
| 145 | + ("a", "b", "c"), |
| 146 | + (True, False) |
| 147 | +) |
| 148 | + |
| 149 | +print(nested[1]) # ('a', 'b', 'c') |
| 150 | +print(nested[1][2]) # 'c' |
| 151 | +``` |
| 152 | + |
| 153 | + |
| 154 | +## Tuple vs. List |
| 155 | + |
| 156 | +| Feature | Tuple | List | |
| 157 | +| ----------- | ------------------------- | ------------------------------ | |
| 158 | +| Syntax | `(1, 2, 3)` | `[1, 2, 3]` | |
| 159 | +| Mutability | Immutable (cannot change) | Mutable (can change) | |
| 160 | +| Methods | count(), index() only | Many built-in methods | |
| 161 | +| Use Case | Fixed data, safe storage | Dynamic data, frequent changes | |
| 162 | +| Performance | Slightly faster | Slightly slower | |
| 163 | + |
| 164 | + |
| 165 | +## When to Use Tuples |
| 166 | + |
| 167 | +**Use tuples when:** |
| 168 | + |
| 169 | +* Data should **not change**. |
| 170 | +* You need **hashable** objects (e.g., as dictionary keys). |
| 171 | +* You want to protect data integrity. |
| 172 | + |
| 173 | +**Examples:** |
| 174 | + |
| 175 | +* Geographic coordinates: `(latitude, longitude)` |
| 176 | +* RGB color codes: `(255, 255, 255)` |
| 177 | +* Database records |
| 178 | + |
| 179 | + |
| 180 | +## Tuple Comprehension |
| 181 | + |
| 182 | +**Note:** Python does NOT have tuple comprehensions. |
| 183 | +However, you can use a **generator expression** in parentheses: |
| 184 | + |
| 185 | +```python |
| 186 | +gen = (x*x for x in range(5)) |
| 187 | +print(gen) # <generator object ...> |
| 188 | +``` |
| 189 | + |
| 190 | +To create a tuple from it, use `tuple()`: |
| 191 | + |
| 192 | +```python |
| 193 | +squares = tuple(x*x for x in range(5)) |
| 194 | +print(squares) # (0, 1, 4, 9, 16) |
| 195 | +``` |
| 196 | + |
| 197 | +## Conclusion |
| 198 | + |
| 199 | +Tuples are a **fundamental** data type in Python, providing a simple, efficient, and immutable way to store ordered data. Understanding when to choose a tuple over a list is essential for writing clear and robust code. |
0 commit comments