Class - XII: SEMESTER – III: Unit – 1: Python Programming: Section 11: Tuples (COMS Only)
Python Programming: Tuples
১. Tuple কী?
Python programming language-এ Tuple হলো একটি built-in data structure, যার মাধ্যমে একাধিক value একসাথে একটি variable-এর মধ্যে store করা যায়। Tuple অনেকটা list-এর মতো, কারণ tuple-ও ordered collection of items। তবে tuple এবং list-এর মধ্যে সবচেয়ে গুরুত্বপূর্ণ পার্থক্য হলো: tuple immutable, অর্থাৎ tuple তৈরি করার পর এর elements পরিবর্তন, add বা remove করা যায় না।
Tuple ব্যবহার করা হয় যখন আমরা এমন data store করতে চাই যা program চলার সময় change হওয়া উচিত নয়।
যেমন coordinates, weekdays, months, fixed settings, employee record, student information, RGB color values ইত্যাদি।
Python-এ tuple সাধারণত parentheses ( ) ব্যবহার করে তৈরি করা হয়।
Example:
numbers = (10, 20, 30, 40)
names = ("Rahim", "Karim", "Salma")
mixed_tuple = (101, "Python", 95.5, True)
print(numbers)
print(names)
print(mixed_tuple)
এখানে numbers, names এবং mixed_tuple — তিনটিই tuple।
Tuple-এর মধ্যে integer, string, float, boolean এমনকি অন্য tuple বা list-ও রাখা যায়।
২. Tuple-এর বৈশিষ্ট্য
- Tuple ordered — elements নির্দিষ্ট order-এ থাকে।
- Tuple immutable — তৈরি হওয়ার পরে elements change করা যায় না।
- Tuple duplicate values allow করে।
- Tuple different data types store করতে পারে।
- Tuple indexing এবং slicing support করে।
- Tuple nested হতে পারে, অর্থাৎ tuple-এর ভিতরে আরেকটি tuple রাখা যায়।
- Tuple list-এর তুলনায় সাধারণত faster এবং memory-efficient।
Duplicate Values Example:
values = (10, 20, 10, 30, 20)
print(values)
এখানে 10 এবং 20 একাধিকবার আছে। Python tuple duplicate values allow করে।
৩. Tuple তৈরি করার বিভিন্ন পদ্ধতি
Python-এ tuple তৈরি করার একাধিক পদ্ধতি রয়েছে। সাধারণত parentheses ( ) ব্যবহার করা হয়,
তবে comma-separated values দিয়েও tuple তৈরি করা যায়।
Parentheses ব্যবহার করে:
numbers = (10, 20, 30)
print(numbers)
Parentheses ছাড়াও tuple তৈরি করা যায়:
numbers = 10, 20, 30
print(numbers)
Single Element Tuple:
single_value = (10,)
print(single_value)
print(type(single_value))
Single element tuple তৈরি করতে value-এর পরে comma , দিতে হয়।
যদি comma না দেওয়া হয়, Python সেটিকে tuple হিসেবে ধরবে না।
Wrong Single Tuple Example:
value = (10)
print(type(value))
এখানে value tuple নয়, এটি integer। তাই single element tuple তৈরি করতে comma জরুরি।
৪. Tuple Indexing
Tuple-এর প্রতিটি element-এর একটি position থাকে, যাকে index বলা হয়।
Python-এ indexing শুরু হয় 0 থেকে। অর্থাৎ প্রথম element-এর index 0, দ্বিতীয় element-এর index 1।
Example:
fruits = ("Apple", "Banana", "Mango", "Orange")
print(fruits[0])
print(fruits[1])
print(fruits[3])
Output:
Apple
Banana
Orange
এখানে fruits[0] প্রথম element return করছে, fruits[1] দ্বিতীয় element return করছে,
এবং fruits[3] চতুর্থ element return করছে।
Negative Indexing:
Python tuple negative indexing support করে। Negative indexing tuple-এর শেষ দিক থেকে শুরু হয়।
শেষ element-এর index -1, তার আগের element-এর index -2।
fruits = ("Apple", "Banana", "Mango", "Orange")
print(fruits[-1])
print(fruits[-2])
print(fruits[-4])
Output:
Orange
Mango
Apple
IndexError হবে।
৫. Tuple Immutable কেন?
Tuple immutable হওয়ার অর্থ হলো tuple তৈরি হওয়ার পরে এর elements directly change করা যায় না। List-এর মতো tuple-এর কোনো element নতুন value দিয়ে replace করা যায় না।
Wrong Example:
numbers = (10, 20, 30)
numbers[1] = 200
print(numbers)
এই code error দেবে, কারণ tuple-এর element assignment support করে না।
Error Type:
TypeError: 'tuple' object does not support item assignment
তবে tuple-এর ভিতরে যদি mutable object যেমন list থাকে, তাহলে সেই inner list পরিবর্তন করা যায়। কারণ tuple-এর reference change হচ্ছে না, inner list-এর content change হচ্ছে।
Example:
data = (10, 20, [30, 40])
data[2][0] = 300
print(data)
Output:
(10, 20, [300, 40])
৬. Tuple Operations
Python tuple-এর উপর বিভিন্ন operation করা যায়। গুরুত্বপূর্ণ tuple operations হলো:
- Concatenation
- Repetition
- Membership
- Slicing
৭. Tuple Concatenation
দুই বা ততোধিক tuple একসাথে যুক্ত করার প্রক্রিয়াকে tuple concatenation বলা হয়।
Python-এ tuple concatenate করার জন্য + operator ব্যবহার করা হয়।
Example:
tuple1 = (10, 20, 30)
tuple2 = (40, 50, 60)
result = tuple1 + tuple2
print(result)
Output:
(10, 20, 30, 40, 50, 60)
এখানে tuple1 এবং tuple2 যুক্ত হয়ে একটি নতুন tuple তৈরি হয়েছে।
মূল tuple দুটি পরিবর্তন হয়নি।
৮. Tuple Repetition
একই tuple একাধিকবার repeat করার জন্য * operator ব্যবহার করা হয়।
Example:
values = (1, 2, 3)
result = values * 3
print(result)
Output:
(1, 2, 3, 1, 2, 3, 1, 2, 3)
এখানে tuple তিনবার repeat হয়েছে এবং একটি নতুন tuple return হয়েছে।
৯. Membership Operation
কোনো value tuple-এর মধ্যে আছে কিনা তা check করতে in এবং not in operators ব্যবহার করা হয়।
Membership operation সাধারণত searching, validation এবং condition checking-এ ব্যবহৃত হয়।
Example:
numbers = (10, 20, 30, 40, 50)
print(30 in numbers)
print(100 in numbers)
print(100 not in numbers)
Output:
True
False
True
Practical Example:
valid_days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
day = input("Day লিখুন: ")
if day in valid_days:
print("Working day")
else:
print("Not a working day")
এখানে fixed weekdays tuple-এর মধ্যে store করা হয়েছে এবং user input valid কিনা check করা হয়েছে।
১০. Tuple Slicing
Tuple-এর নির্দিষ্ট অংশ বের করার জন্য slicing ব্যবহার করা হয়। Slicing-এর মাধ্যমে tuple-এর subset বা portion পাওয়া যায়।
Syntax:
tuple_name[start : stop : step]
| Part | ব্যাখ্যা |
|---|---|
start |
কোন index থেকে শুরু হবে |
stop |
কোন index-এর আগ পর্যন্ত যাবে |
step |
কত ধাপ করে এগোবে |
Example:
numbers = (10, 20, 30, 40, 50, 60, 70)
print(numbers[1:5])
print(numbers[:4])
print(numbers[3:])
print(numbers[::2])
print(numbers[::-1])
Output:
(20, 30, 40, 50)
(10, 20, 30, 40)
(40, 50, 60, 70)
(10, 30, 50, 70)
(70, 60, 50, 40, 30, 20, 10)
numbers[::-1] tuple-কে reverse order-এ return করে।
১১. Traversing a Tuple using Loops
Tuple-এর প্রতিটি element একে একে access বা process করার প্রক্রিয়াকে tuple traversal বলা হয়।
Python-এ tuple traverse করার জন্য সাধারণত for loop এবং while loop ব্যবহার করা হয়।
Using for Loop:
numbers = (10, 20, 30, 40)
for num in numbers:
print(num)
Using index with for Loop:
numbers = (10, 20, 30, 40)
for i in range(len(numbers)):
print("Index:", i, "Value:", numbers[i])
Using while Loop:
numbers = (10, 20, 30, 40)
i = 0
while i < len(numbers):
print(numbers[i])
i = i + 1
Direct element access করতে for num in numbers সহজ। কিন্তু index দরকার হলে range(len(tuple)) ব্যবহার করা যায়।
১২. len()
len() function tuple-এর total element সংখ্যা return করে।
Example:
numbers = (10, 20, 30, 40, 50)
print(len(numbers))
Output:
5
এখানে tuple-এর মধ্যে ৫টি element আছে, তাই output 5।
১৩. tuple()
tuple() function ব্যবহার করে অন্য iterable object যেমন list, string, range ইত্যাদিকে tuple-এ convert করা যায়।
List থেকে Tuple:
numbers_list = [10, 20, 30]
numbers_tuple = tuple(numbers_list)
print(numbers_tuple)
Output:
(10, 20, 30)
String থেকে Tuple:
text = "Python"
letters = tuple(text)
print(letters)
Output:
('P', 'y', 't', 'h', 'o', 'n')
range থেকে Tuple:
numbers = tuple(range(1, 6))
print(numbers)
Output:
(1, 2, 3, 4, 5)
১৪. count()
count() method tuple-এর মধ্যে কোনো element কতবার আছে তা return করে।
Example:
numbers = (10, 20, 10, 30, 10, 40)
print(numbers.count(10))
print(numbers.count(20))
print(numbers.count(100))
Output:
3
1
0
এখানে 10 তিনবার আছে, 20 একবার আছে এবং 100 নেই, তাই count 0।
১৫. index()
index() method tuple-এর মধ্যে কোনো element-এর প্রথম occurrence-এর index return করে।
যদি element tuple-এর মধ্যে না থাকে, তাহলে ValueError দেয়।
Example:
numbers = (10, 20, 30, 40)
print(numbers.index(30))
Output:
2
Safe index check:
numbers = (10, 20, 30, 40)
value = 50
if value in numbers:
print(numbers.index(value))
else:
print("Value tuple-এর মধ্যে নেই")
Safe checking-এর জন্য আগে membership operator in ব্যবহার করা ভালো।
১৬. sorted()
sorted() function tuple-এর elements sort করে একটি নতুন list return করে।
এটি original tuple change করে না, কারণ tuple immutable।
Example:
numbers = (40, 10, 30, 20)
sorted_numbers = sorted(numbers)
print(sorted_numbers)
print(numbers)
Output:
[10, 20, 30, 40]
(40, 10, 30, 20)
এখানে sorted_numbers একটি list, আর original tuple unchanged আছে।
Descending order:
numbers = (40, 10, 30, 20)
sorted_numbers = sorted(numbers, reverse=True)
print(sorted_numbers)
Output:
[40, 30, 20, 10]
tuple(sorted(numbers)) ব্যবহার করা যায়।
numbers = (40, 10, 30, 20)
sorted_tuple = tuple(sorted(numbers))
print(sorted_tuple)
১৭. min(), max() এবং sum()
Numeric tuple-এর উপর common mathematical operations করার জন্য min(), max(), এবং sum() খুব useful।
| Function | কাজ | Example |
|---|---|---|
min() |
Tuple-এর minimum value return করে | min((10, 20, 5)) → 5 |
max() |
Tuple-এর maximum value return করে | max((10, 20, 5)) → 20 |
sum() |
Tuple-এর numeric values যোগ করে | sum((10, 20, 5)) → 35 |
Example:
marks = (80, 75, 90, 65, 88)
print("Minimum:", min(marks))
print("Maximum:", max(marks))
print("Total:", sum(marks))
print("Average:", sum(marks) / len(marks))
Output:
Minimum: 65
Maximum: 90
Total: 398
Average: 79.6
sum() শুধু numeric values-এর উপর কাজ করে। Tuple-এর মধ্যে string বা mixed data থাকলে error হতে পারে।
১৮. Tuple Assignment
Python-এ tuple assignment হলো এমন একটি technique যার মাধ্যমে একসাথে একাধিক variable-এ value assign করা যায়। এটি code ছোট, readable এবং efficient করে।
Basic Tuple Assignment:
name, age, marks = ("Rahim", 20, 85)
print(name)
print(age)
print(marks)
Output:
Rahim
20
85
এখানে tuple-এর তিনটি value তিনটি variable-এ assign হয়েছে। এটাকে tuple unpacking-ও বলা হয়।
Parentheses ছাড়াও কাজ করে:
name, age, marks = "Karim", 21, 90
print(name, age, marks)
Variable Swapping:
Tuple assignment ব্যবহার করে temporary variable ছাড়াই দুইটি variable-এর value swap করা যায়।
a = 10
b = 20
a, b = b, a
print("a =", a)
print("b =", b)
Output:
a = 20
b = 10
Multiple Return Values from Function:
def calculate(a, b):
total = a + b
difference = a - b
product = a * b
return total, difference, product
s, d, p = calculate(10, 5)
print("Sum:", s)
print("Difference:", d)
print("Product:", p)
Python function একাধিক value return করলে তা আসলে tuple আকারে return হয়। Tuple assignment ব্যবহার করে আমরা values আলাদা variable-এ store করতে পারি।
ValueError হতে পারে।
Wrong Example:
a, b = (10, 20, 30)
এখানে tuple-এ তিনটি value আছে কিন্তু variable দুইটি, তাই error হবে।
১৯. Nested Tuple
একটি tuple-এর ভিতরে আরেকটি tuple থাকলে তাকে Nested Tuple বলা হয়। Nested tuple ব্যবহার করা হয় complex data structure তৈরি করতে, যেমন student records, matrix-like data, employee details, coordinates group ইত্যাদি।
Example:
students = (
("Rahim", 20, 85),
("Karim", 21, 90),
("Salma", 19, 88)
)
print(students)
এখানে students একটি nested tuple, যেখানে প্রতিটি inner tuple একজন student-এর name, age এবং marks store করছে।
Nested Tuple Element Access:
students = (
("Rahim", 20, 85),
("Karim", 21, 90),
("Salma", 19, 88)
)
print(students[0])
print(students[0][0])
print(students[1][2])
print(students[2][1])
Output:
('Rahim', 20, 85)
Rahim
90
19
students[1][2] মানে দ্বিতীয় student-এর তৃতীয় value, অর্থাৎ marks।
Nested Tuple Traversal:
students = (
("Rahim", 20, 85),
("Karim", 21, 90),
("Salma", 19, 88)
)
for student in students:
name, age, marks = student
print("Name:", name)
print("Age:", age)
print("Marks:", marks)
print("-----")
এখানে প্রতিটি inner tuple tuple assignment ব্যবহার করে name, age, এবং marks variable-এ unpack করা হয়েছে।
২০. Tuple Methods and Functions Summary Table
| Function / Method | কাজ | Example |
|---|---|---|
len() |
Tuple-এর length return করে | len((10, 20, 30)) → 3 |
tuple() |
Iterable object-কে tuple-এ convert করে | tuple([1, 2, 3]) |
count() |
Element কতবার আছে count করে | (1, 2, 1).count(1) → 2 |
index() |
Element-এর প্রথম index return করে | (10, 20, 30).index(20) → 1 |
sorted() |
Tuple sort করে নতুন list return করে | sorted((3, 1, 2)) → [1, 2, 3] |
min() |
Minimum value return করে | min((10, 5, 20)) → 5 |
max() |
Maximum value return করে | max((10, 5, 20)) → 20 |
sum() |
Numeric tuple-এর total return করে | sum((10, 20, 30)) → 60 |
২১. Tuple vs List
Tuple এবং list দেখতে অনেকটা একই রকম হলেও এদের ব্যবহার এবং behavior আলাদা। List mutable, কিন্তু tuple immutable। তাই যেখানে data পরিবর্তন করতে হবে সেখানে list এবং যেখানে fixed data রাখতে হবে সেখানে tuple ব্যবহার করা ভালো।
| বিষয় | Tuple | List |
|---|---|---|
| Syntax | (10, 20, 30) |
[10, 20, 30] |
| Mutability | Immutable | Mutable |
| Element change | Directly change করা যায় না | Change করা যায় |
| Methods | কম methods আছে | অনেক methods আছে |
| Use Case | Fixed data, records, constants | Dynamic data, editable collection |
| Performance | Generally faster | Generally flexible but slightly heavier |
২২. Practical Program ১: Tuple থেকে Maximum, Minimum, Sum এবং Average বের করা
Numeric tuple-এর data থেকে maximum, minimum, sum এবং average বের করা tuple-এর একটি common practical use case।
Program:
numbers = (10, 25, 5, 40, 30)
maximum_value = max(numbers)
minimum_value = min(numbers)
total = sum(numbers)
average = total / len(numbers)
print("Tuple:", numbers)
print("Maximum:", maximum_value)
print("Minimum:", minimum_value)
print("Sum:", total)
print("Average:", average)
Output:
Tuple: (10, 25, 5, 40, 30)
Maximum: 40
Minimum: 5
Sum: 110
Average: 22.0
২৩. Practical Program ২: Tuple-এর Element Frequency Count
Tuple-এর মধ্যে কোনো value কতবার আছে তা count করতে count() method ব্যবহার করা যায়।
Program:
numbers = (10, 20, 10, 30, 20, 10, 40)
checked = ()
for value in numbers:
if value not in checked:
frequency = numbers.count(value)
print(value, "appears", frequency, "times")
checked = checked + (value,)
Output:
10 appears 3 times
20 appears 2 times
30 appears 1 times
40 appears 1 times
যেহেতু tuple immutable, তাই checked tuple-এ নতুন value add করার জন্য concatenation ব্যবহার করা হয়েছে।
২৪. Practical Program ৩: Student Records using Nested Tuple
Nested tuple ব্যবহার করে fixed student records store করা যায়। প্রতিটি inner tuple একজন student-এর information represent করে।
Program:
students = (
("Rahim", 20, 85),
("Karim", 21, 90),
("Salma", 19, 88),
("Jamal", 22, 76)
)
highest_marks = students[0][2]
top_student = students[0][0]
for student in students:
name, age, marks = student
if marks > highest_marks:
highest_marks = marks
top_student = name
print("Top Student:", top_student)
print("Highest Marks:", highest_marks)
Explanation:
- Nested tuple-এ student records রাখা হয়েছে।
- প্রথম student-এর marks initial highest হিসেবে নেওয়া হয়েছে।
- Loop দিয়ে প্রতিটি student-এর marks check করা হয়েছে।
- যদি কোনো student-এর marks বেশি হয়, তাহলে top student update হয়েছে।
- শেষে highest marks এবং student name print করা হয়েছে।
২৫. Common Mistakes in Tuples
- Single element tuple-এ comma না দেওয়া:
(10)tuple নয়,(10,)tuple। - Tuple modify করার চেষ্টা করা: tuple immutable, তাই element change করা যায় না।
- index() value না থাকলে error দেয়: safe checking-এর জন্য আগে
inব্যবহার করা ভালো। - sorted() tuple return করে ভাবা:
sorted()list return করে, tuple নয়। - sum() mixed tuple-এ ব্যবহার করা: numeric values না থাকলে
sum()error দিতে পারে। - Tuple unpacking-এ variable সংখ্যা ভুল রাখা: variable এবং values সংখ্যা match না করলে
ValueErrorহবে।
Safe Example:
numbers = (10, 20, 30)
value = 40
if value in numbers:
print(numbers.index(value))
else:
print("Value not found")
২৬. Complete Practice Program
নিচের program-এ tuple creation, indexing, slicing, membership, count, index, sorted, min, max, sum, tuple assignment এবং nested tuple — একসাথে দেখানো হয়েছে।
numbers = (10, 20, 30, 20, 40, 50, 20)
print("Original Tuple:", numbers)
print("First element:", numbers[0])
print("Last element:", numbers[-1])
print("Slice from index 1 to 4:", numbers[1:5])
print("Length:", len(numbers))
print("Count of 20:", numbers.count(20))
value = 30
if value in numbers:
print(value, "found at index:", numbers.index(value))
else:
print(value, "not found")
print("Sorted list:", sorted(numbers))
print("Minimum:", min(numbers))
print("Maximum:", max(numbers))
print("Sum:", sum(numbers))
print("Average:", sum(numbers) / len(numbers))
a, b, c = (100, 200, 300)
print("Tuple Assignment Values:", a, b, c)
students = (
("Rahim", 20, 85),
("Karim", 21, 90),
("Salma", 19, 88)
)
print("Student Records:")
for student in students:
name, age, marks = student
print(name, age, marks)
Program Explanation:
- প্রথমে একটি numeric tuple তৈরি করা হয়েছে।
- Indexing এবং negative indexing দিয়ে elements access করা হয়েছে।
- Slicing ব্যবহার করে tuple-এর একটি অংশ নেওয়া হয়েছে।
len(),count(),index(),sorted(),min(),max(),sum()ব্যবহার করা হয়েছে।- Tuple assignment ব্যবহার করে একাধিক variable-এ value assign করা হয়েছে।
- Nested tuple ব্যবহার করে student records store এবং traverse করা হয়েছে।
২৭. উপসংহার
Python tuple একটি গুরুত্বপূর্ণ data structure, যা ordered এবং immutable collection হিসেবে কাজ করে। Tuple list-এর মতো multiple values store করতে পারে, কিন্তু tuple-এর values তৈরি হওয়ার পরে সরাসরি পরিবর্তন করা যায় না। তাই fixed বা constant data store করার জন্য tuple খুবই useful।
Tuple indexing, slicing, concatenation, repetition এবং membership operations support করে।
Built-in functions এবং methods যেমন len(), tuple(), count(), index(),
sorted(), min(), max() এবং sum() tuple processing-কে সহজ করে।
Tuple assignment Python-এর একটি powerful feature, যার মাধ্যমে একসাথে একাধিক variable-এ value assign করা যায় এবং variable swapping সহজ হয়।
Nested tuple ব্যবহার করে structured records যেমন student data, employee data, coordinates এবং tabular information store করা যায়। একজন beginner programmer-এর জন্য tuple ভালোভাবে শেখা জরুরি, কারণ এটি data safety, efficient storage এবং clean code writing-এর জন্য খুব গুরুত্বপূর্ণ।