# টাপল

ইতোমধ্যে দেখে আসা লিস্টের মতই আরেকটি ডাটা স্ট্রাকচার হচ্ছে Tuple. কিন্তু গুরুত্বপূর্ণ পার্থক্যটি হচ্ছে এটি Immutable টাইপের অর্থাৎ, এর ভ্যালু পরিবর্তন করা যায় না। আবার, লিস্ট যেমন তৈরি করতে হয় দুটো `[]` ব্র্যাকেট দিয়ে কিন্তু টাপল তৈরি করতে হয় `()` দিয়ে (যদিও ব্র্যাকেট ছাড়াও শুধু কমা চিহ্ন দিয়ে ভ্যালু গুলোকে আলাদা করেও টাপল তৈরি করা যায়)। আরও কিছু পার্থক্য আছে এবং অবশ্যই লিস্ট ব্যবহার না করে কিছু যায়গায় কেন টাপল ব্যবহার করা উচিৎ তার কিছু কারনও আছে। সেগুলো এখানে আলোচনা করা হবে।

উদাহরণ,

```python
roles = ("Admin", "Operator", "User")   
# Or following line will create the same Tuple
# roles = "Admin", "Operator", "User" 

print(roles[0])
```

আউটপুট,

```python
Admin
```

আমরা দেখতে পাচ্ছি টাপল থেকে ভ্যালু অ্যাক্সেসের জন্যও লিস্টের মতই ইনডেক্স ব্যবহার করা যায়। কিন্তু লিস্টের মত নতুন ভ্যালু যুক্ত বা আপডেট করা যায় না। যেমন -

```python
roles = ("Admin", "Operator", "User")   
roles[2] = "Customer"
```

আউটপুট,

```python
TypeError: 'tuple' object does not support item assignment
```

> খুব স্বাভাবিক ভাবেই `roles = ()` এভাবে ফাকা একটি টাপল ইনিসিয়ালাইজ করা যায়.

একটি টাপলের মধ্যে ভ্যালু হিসেবে অন্য লিস্ট, ডিকশনারি বা টাপল থাকতে পারে। যেমন -

```python
permissions = (("Admin", "Operator", "Customer"), ("Developer", "Tester"), [1, 2, 3], {"Stage": "Development"})

print(permissions[3]["Stage"])
```

আউটপুট,

```python
Development
```

**টাপল আনপ্যাকিং**\
টাপল আনপ্যাকিং এর মাধ্যমে একটি টাপলের (বা যেকোনো ইটারেবল) মধ্যে থাকা প্রত্যেকটি ভ্যালুকে আলাদা আলাদা নতুন ভ্যারিয়েবলে অ্যাসাইন করা যায় এক লাইন কোড লিখেই। নিচের উদাহরণ দেখি,

```python
numbers = (1, 2, 3)
a, b, c = numbers
print(a)
print(b)
print(c)
```

অর্থাৎ `numbers` নামের টাপলের তিনটি ভ্যালুকে পরের লাইনে আলাদা আলাদা তিনটি ভ্যারিয়েবল `a`, `b`, `c` তে জমা রাখা হয়েছে।

আউটপুট,

```python
1
2
3
```

যদি এমন হয় যে একটি টাপল বা ইটারেবলে অসংখ্য ভ্যালু আছে কিন্তু আমরা এগুলো অল্প কিছু আলাদা আলাদা ভ্যারিয়েবলে জমা রাখতে চাই। অর্থাৎ, এর জন্য সমান চিহ্নের বাম পাশে অসংখ্য ভ্যারিয়েবল লিখতে চাই না তখন নিচের মত করে যেকোনো ভ্যারিয়েবলের সামনে `*` যুক্ত করে অবশিষ্ট যেকোনো সংখ্যক ভ্যালুকে এর মধ্যে জমা রাখা যায়।

উদাহরণ,

```python
a, b, *c, d = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a)
print(b)
print(c)
print(d)
```

এখানে `1` জমা হচ্ছে `a` এর মধ্যে, `2` জমা হচ্ছে `b` এর মধ্যে কিন্তু এরপর থেকে বাকিগুলো জমা হচ্ছে `c` এর মধ্যে। আর ডান পাশের ইটারেবলের শেষ ভ্যালু জমা হচ্ছে বাম পাশের শেষ ভ্যারিয়েবল `d` এর মধ্যে।

আউটপুট,

```python
1
2
[3, 4, 5, 6, 7, 8]
9
```

> সংকলন - [নুহিল মেহেদী](https://nuhil.net)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://python.howtocode.dev/data-types/tuple.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
