Tuple, Set ও Dictionary
🐍 Python Tuple, Set ও Dictionary – সম্পূর্ণ গাইড
Python এ ডাটা সংরক্ষণের জন্য Tuple, Set এবং Dictionary খুব গুরুত্বপূর্ণ ডাটা স্ট্রাকচার। এই টিউটোরিয়ালে আমরা প্রতিটি ডাটা টাইপের পরিচিতি, item access, change, remove এবং গুরুত্বপূর্ণ method গুলো উদাহরণসহ শিখবো।
📌 1. Python Tuple কী?
Tuple হলো ordered এবং immutable ডাটা টাইপ। অর্থাৎ Tuple তৈরি হওয়ার পরে এর item পরিবর্তন করা যায় না।
✔ Tuple তৈরি
numbers = (10, 20, 30, 40) print(numbers) # Output: # (10, 20, 30, 40)
✔ Tuple থেকে item access
print(numbers[1]) print(numbers[-1]) # Output: # 20 # 40
❌ Tuple item change করা যায় না
numbers[0] = 100 # Output: # TypeError: 'tuple' object does not support item assignment
✔ Tuple useful methods
- count() – কতবার আছে
- index() – index খুঁজে বের করা
nums = (1, 2, 2, 3) print(nums.count(2)) print(nums.index(3)) # Output: # 2 # 3
📌 2. Python Set কী?
Set হলো unordered এবং unique ডাটা টাইপ। এখানে duplicate value রাখা যায় না।
✔ Set তৈরি
fruits = {"apple", "banana", "apple"}
print(fruits)
# Output:
# {'apple', 'banana'}
✔ Set item access
Set এ index নেই, loop ব্যবহার করে access করতে হয়।
for item in fruits:
print(item)
# Output:
# apple
# banana
✔ Set item add / change
fruits.add("mango")
print(fruits)
# Output:
# {'apple', 'banana', 'mango'}
✔ Set item remove
fruits.remove("banana")
print(fruits)
# Output:
# {'apple', 'mango'}
✔ Set useful methods
- add()
- remove()
- discard()
- union()
- intersection()
📌 3. Python Dictionary কী?
Dictionary হলো key : value ভিত্তিক ডাটা টাইপ। এটি সবচেয়ে বেশি ব্যবহার হয় real-world application এ।
✔ Dictionary তৈরি
student = {
"name": "Rahul",
"age": 21,
"course": "Python"
}
print(student)
# Output:
# {'name': 'Rahul', 'age': 21, 'course': 'Python'}
✔ Dictionary item access
print(student["name"])
print(student.get("age"))
# Output:
# Rahul
# 21
✔ Dictionary item change / update
student["age"] = 22
student.update({"course": "Django"})
print(student)
# Output:
# {'name': 'Rahul', 'age': 22, 'course': 'Django'}
✔ Dictionary item remove
student.pop("age")
del student["course"]
print(student)
# Output:
# {'name': 'Rahul'}
✔ Dictionary useful methods
- keys()
- values()
- items()
- pop()
- clear()
print(student.keys()) print(student.values()) # Output: # dict_keys(['name']) # dict_values(['Rahul'])
✨ সংক্ষেপে মনে রাখবেন
- Tuple → Immutable (পরিবর্তন করা যায় না)
- Set → Unique value, unordered
- Dictionary → Key-Value structure
লোড হচ্ছে...
1. Tuple কী?
Tuple হলো অপরিবর্তনযোগ্য (immutable) ডাটা টাইপ।
2. Set এর বৈশিষ্ট্য কী?
Set এ ডুপ্লিকেট ভ্যালু রাখা যায় না এবং এটি unordered।
3. Dictionary কী?
Dictionary key-value pair আকারে ডাটা সংরক্ষণ করে।