Skip to content

Commit 8f098ab

Browse files
author
majid shahbaz
committed
Added tuples tutorial
1 parent 2cf8c47 commit 8f098ab

File tree

3 files changed

+550
-8
lines changed

3 files changed

+550
-8
lines changed

README.md

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,27 +126,58 @@
126126
4.2.4.2. Matrix Transpose (List Comprehension)
127127
4.2.4.3. Matrix Transformation Algorithms
128128

129-
#### [4.3. Dictionaries]()
129+
#### [4.3. Dictionaries](https://github.com/Majid460/python_practice_part1/blob/main/src/datastructures/dict/dict.py)
130130

131131
#### Quick Overview
132-
4.3.1. Dictionary Basics and Creation**
132+
4.3.1. Dictionary Basics and Creation
133133
4.3.1.1. Empty Dictionary Creation
134134
4.3.1.2. Dictionary with Elements
135135
4.3.1.3. Constructor Method
136-
4.3.2. Accessing Dictionary Elements**
136+
4.3.2. Accessing Dictionary Elements
137137
4.3.2.1. Direct Key Access
138138
4.3.2.2. Multiple Access Methods
139139
4.3.2.3. Loop Iterations
140140
4.3.2.4. Dictionary Comprehension
141-
4.3.3. Dictionary Properties**
141+
4.3.3. Dictionary Properties
142142
4.3.3.1. Ordering Behavior
143143
4.3.3.2. Key-Value Relationships
144-
4.3.4. Modifying Dictionaries**
144+
4.3.4. Modifying Dictionaries
145145
4.3.4.1. Update Method
146146
4.3.4.2. Element Removal (pop, popitem, del, clear)
147-
4.3.5. Dictionary Copying**
147+
4.3.5. Dictionary Copying
148148
4.3.5.1. Shallow vs Deep Copy
149149
4.3.5.2. Copy Methods
150-
4.3.6. Nested Dictionaries**
150+
4.3.6. Nested Dictionaries
151151
4.3.6.1. Structure and Access
152-
4.3.6.2. Nested Iteration
152+
4.3.6.2. Nested Iteration
153+
154+
#### [4.4. Tuples](https://github.com/Majid460/python_practice_part1/blob/main/src/datastructures/tuples/tuples.py)
155+
156+
#### Quick Overview
157+
4.4.1. Tuple Basics and Creation
158+
4.4.1.1. Basic Tuple Creation
159+
4.4.1.2. Mixed Data Types
160+
4.4.1.3. Tuple vs List Differences
161+
4.4.2. Tuple Operations
162+
4.4.2.1. Index Access (Positive/Negative)
163+
4.4.2.2. Range Slicing
164+
4.4.2.3. Membership Testing
165+
4.4.3. Immutability and Workarounds
166+
4.4.3.1. Converting to List for Modification
167+
4.4.3.2. Adding Items (List Conversion)
168+
4.4.3.3. Removing Items (List Conversion)
169+
4.4.3.4. Deleting Entire Tuple
170+
4.4.4. Tuple Unpacking**
171+
4.4.4.1. Basic Unpacking
172+
4.4.4.2. Asterisk (*) Unpacking
173+
4.4.4.3. Advanced Asterisk Patterns
174+
4.4.5. Looping Through Tuples
175+
4.4.5.1. Index-Based For Loop
176+
4.4.5.2. Simple For Loop
177+
4.4.5.3. While Loop
178+
4.4.6. Joining Tuples
179+
4.4.6.1. Concatenation with + Operator
180+
4.4.6.2. Multiplication with * Operator
181+
4.4.7. Tuple Methods
182+
4.4.7.1. count() Method
183+
4.4.7.2. index() Method
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
# Python Tuple Tutorial
2+
3+
A comprehensive guide to understanding and working with tuples in Python.
4+
5+
## Table of Contents
6+
- [What are Tuples?](#what-are-tuples)
7+
- [Tuple Characteristics](#tuple-characteristics)
8+
- [Creating Tuples](#creating-tuples)
9+
- [Basic Tuple Operations](#basic-tuple-operations)
10+
- [Immutability and Workarounds](#immutability-and-workarounds)
11+
- [Tuple Unpacking](#tuple-unpacking)
12+
- [Looping Through Tuples](#looping-through-tuples)
13+
- [Joining Tuples](#joining-tuples)
14+
- [Tuple Methods](#tuple-methods)
15+
16+
## What are Tuples?
17+
18+
Tuples are Python's built-in data structure for storing **multiple items in a single variable**. They are similar to lists but with one crucial difference: **immutability**.
19+
20+
### Basic Template
21+
```python
22+
this_tuple = ("a", "b", "c", 1, 4)
23+
```
24+
25+
## Tuple Characteristics
26+
27+
| Feature | Tuple | List |
28+
|---------|-------|------|
29+
| **Ordered** | ✅ Yes | ✅ Yes |
30+
| **Mutable** | ❌ No (Immutable) | ✅ Yes (Mutable) |
31+
| **Allow Duplicates** | ✅ Yes | ✅ Yes |
32+
| **Syntax** | `()` Round brackets | `[]` Square brackets |
33+
| **Indexing** | ✅ Yes | ✅ Yes |
34+
35+
## Creating Tuples
36+
37+
```python
38+
# Basic tuple with mixed data types
39+
this_tuple = ("a", "b", "c", 1, 4)
40+
print(f"This is basic tuple: {this_tuple}")
41+
# Output: ('a', 'b', 'c', 1, 4)
42+
43+
print(f"Tuple type: {type(this_tuple)}")
44+
# Output: <class 'tuple'>
45+
```
46+
47+
## Basic Tuple Operations
48+
49+
### 1. Index Access
50+
```python
51+
this_tuple = ("a", "b", "c", 1, 4)
52+
53+
# Positive indexing
54+
print(f"Index 1: {this_tuple[1]}") # b
55+
56+
# Negative indexing (-1 means last element)
57+
print(f"Last element: {this_tuple[-1]}") # 4
58+
```
59+
60+
### 2. Range Slicing
61+
```python
62+
# Range slicing (excludes end index)
63+
print(f"Range 1:4: {this_tuple[1:4]}") # ('b', 'c', 1)
64+
65+
# Negative range slicing
66+
print(f"Negative range -4:-1: {this_tuple[-4:-1]}") # ('b', 'c', 1)
67+
```
68+
69+
### 3. Membership Testing
70+
```python
71+
item = "Apple"
72+
tup = ("Banana", "Apple", "Orange", "Peach")
73+
74+
if item in tup:
75+
print(f"{item} is in the tuple")
76+
# Output: Apple is in the tuple
77+
```
78+
79+
## Immutability and Workarounds
80+
81+
Tuples are **immutable** - you cannot change them after creation. However, there are workarounds:
82+
83+
### 1. Changing Items (Convert to List)
84+
```python
85+
tup = ("Banana", "Apple", "Orange", "Peach")
86+
87+
# Convert to list → modify → convert back
88+
list_tup = list(tup)
89+
list_tup[1] = "Pear"
90+
tup_modified = tuple(list_tup)
91+
92+
print(f"After changing: {tup_modified}")
93+
# Output: ('Banana', 'Pear', 'Orange', 'Peach')
94+
```
95+
96+
### 2. Adding Items
97+
98+
#### Method 1: Convert to List
99+
```python
100+
tuple_original = ("Banana", "Pear", "Orange", "Peach")
101+
102+
# Convert → add → convert back
103+
temp_list = list(tuple_original)
104+
temp_list.append("Grapes")
105+
tuple_with_addition = tuple(temp_list)
106+
107+
print(f"After adding: {tuple_with_addition}")
108+
# Output: ('Banana', 'Pear', 'Orange', 'Peach', 'Grapes')
109+
```
110+
111+
#### Method 2: Tuple Concatenation
112+
```python
113+
tuple_a = ("q", "s", "d")
114+
tuple_b = ("r", "f", "g")
115+
116+
tuple_a += tuple_b # Creates new tuple
117+
print(f"Concatenated: {tuple_a}")
118+
# Output: ('q', 's', 'd', 'r', 'f', 'g')
119+
```
120+
121+
### 3. Removing Items
122+
```python
123+
tuple_original = ("Banana", "Pear", "Orange", "Peach", "Grapes")
124+
125+
# Convert → remove → convert back
126+
temp_list = list(tuple_original)
127+
temp_list.remove("Grapes")
128+
tuple_after_removal = tuple(temp_list)
129+
130+
print(f"After removing Grapes: {tuple_after_removal}")
131+
# Output: ('Banana', 'Pear', 'Orange', 'Peach')
132+
```
133+
134+
### 4. Deleting Entire Tuple
135+
```python
136+
my_tuple = ("a", "b", "c")
137+
del my_tuple # Deletes the entire tuple object
138+
# print(my_tuple) # This would cause NameError
139+
```
140+
141+
## Tuple Unpacking
142+
143+
**Unpacking** allows you to assign tuple values to individual variables:
144+
145+
### 1. Basic Unpacking
146+
```python
147+
tup = ("Banana", "Apple", "Orange", "Peach")
148+
(red, green, blue, white) = tup
149+
150+
print(red) # Banana
151+
print(green) # Apple
152+
print(blue) # Orange
153+
print(white) # Peach
154+
```
155+
156+
### 2. Using Asterisk (*) - Collect Remaining Items
157+
```python
158+
# When variables < values, use *
159+
(red, *green) = tup
160+
print(red) # Banana
161+
print(green) # ['Apple', 'Orange', 'Peach']
162+
```
163+
164+
### 3. Asterisk in Middle
165+
```python
166+
fruits = ("apple", "mango", "papaya", "pineapple", "cherry")
167+
(green, *tropic, red) = fruits
168+
169+
print(green) # apple
170+
print(tropic) # ['mango', 'papaya', 'pineapple']
171+
print(red) # cherry
172+
```
173+
174+
## Looping Through Tuples
175+
176+
### 1. Index-Based Loop
177+
```python
178+
fruits = ("apple", "mango", "papaya", "pineapple", "cherry")
179+
180+
for i in range(len(fruits)):
181+
print(fruits[i])
182+
```
183+
184+
### 2. Simple For Loop
185+
```python
186+
for fruit in fruits:
187+
print(fruit)
188+
```
189+
190+
### 3. While Loop
191+
```python
192+
i = 0
193+
while i < len(fruits):
194+
print(fruits[i])
195+
i += 1
196+
```
197+
198+
## Joining Tuples
199+
200+
### 1. Using + Operator
201+
```python
202+
fruits = ("apple", "mango", "papaya")
203+
more_fruits = ("Banana", "Pear", "Orange")
204+
205+
all_fruits = fruits + more_fruits
206+
print(all_fruits)
207+
# Output: ('apple', 'mango', 'papaya', 'Banana', 'Pear', 'Orange')
208+
```
209+
210+
### 2. Using * Operator (Multiplication)
211+
```python
212+
fruits = ("apple", "mango", "papaya")
213+
doubled_fruits = fruits * 2
214+
215+
print(doubled_fruits)
216+
# Output: ('apple', 'mango', 'papaya', 'apple', 'mango', 'papaya')
217+
```
218+
219+
## Tuple Methods
220+
221+
Python provides **only 2 built-in methods** for tuples (due to immutability):
222+
223+
### 1. `count()` - Count Occurrences
224+
```python
225+
this_tuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
226+
227+
count_of_5 = this_tuple.count(5)
228+
print(f"5 appears {count_of_5} times")
229+
# Output: 5 appears 2 times
230+
```
231+
232+
### 2. `index()` - Find First Occurrence
233+
```python
234+
this_tuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
235+
236+
first_8_index = this_tuple.index(8)
237+
print(f"First occurrence of 8 is at index {first_8_index}")
238+
# Output: First occurrence of 8 is at index 3
239+
```
240+
241+
**⚠️ Warning**: `index()` raises an exception if the value is not found!
242+
243+
## Key Differences: Tuple vs List
244+
245+
| Aspect | Tuple | List |
246+
|--------|-------|------|
247+
| **Mutability** | Immutable (cannot change) | Mutable (can change) |
248+
| **Performance** | Faster (optimized) | Slightly slower |
249+
| **Use Case** | Fixed data, coordinates | Dynamic data |
250+
| **Methods** | Only 2 (`count`, `index`) | Many (`append`, `remove`, etc.) |
251+
| **Memory** | Less memory usage | More memory usage |
252+
253+
## When to Use Tuples
254+
255+
1. **Fixed data**: Coordinates `(x, y)`, RGB colors `(255, 0, 0)`
256+
2. **Dictionary keys**: Tuples can be dict keys (immutable), lists cannot
257+
3. **Function returns**: Return multiple values from functions
258+
4. **Data integrity**: When you want to prevent accidental changes
259+
5. **Performance**: When you need faster iteration over immutable data
260+
261+
## Best Practices
262+
263+
1. **Use tuples for fixed collections** that won't change
264+
2. **Use lists for dynamic collections** that need modification
265+
3. **Remember the conversion pattern**: `tuple → list → modify → tuple`
266+
4. **Use unpacking** to make code more readable
267+
5. **Handle exceptions** when using `index()` method
268+
269+
## Common Use Cases
270+
271+
```python
272+
# Coordinates
273+
point = (10, 20)
274+
x, y = point
275+
276+
# RGB Color
277+
color = (255, 0, 128)
278+
red, green, blue = color
279+
280+
# Database record
281+
record = ("John", 25, "Engineer", 50000)
282+
name, age, job, salary = record
283+
284+
# Multiple return values
285+
def get_name_age():
286+
return "Alice", 30
287+
288+
name, age = get_name_age()
289+
```
290+
291+
---
292+
293+
**Remember**: Tuples are **immutable by design** - this is a feature, not a limitation! Use them when you want to ensure data doesn't accidentally change.

0 commit comments

Comments
 (0)