পাইথন প্রোগ্রামিং : তালিকা (Python Lists in Bangla)

1.11 Python Lists

Python এর সবচেয়ে বেসিক ডাটা স্ট্রাকচার হচ্ছে sequence. এর প্রতিটি উপাদান এর সাথে একটি নাম্বার assign করা থাকে, যা এর অবস্থান/ ইন্ডেক্স নির্দেশ করে। সর্ব প্রথম ইন্ডেক্স হচ্ছে ০, তারপরেরটা ১, তারপরেরটা ২... ইত্যাদি !

Python এ ৬ ধরনের built-in sequence আছে, তাদের মধ্যে সবচেয়ে প্রচলিত হচ্ছে lists আর tuples। sequence টাইপের সাহায্যে indexing, slicing, adding, multiplying, ও checking করা যায়। এছাড়াও Python এর বিল্ট-ইন ফাংশনের সাহায্যে যেকোনো sequence এর দৈর্ঘ্য হিসেব করা যায় যার সাহায্যে দীর্ঘতম ও ক্ষুদ্রতম প্রোসেস বের করা যায়।

 

Python Lists

List হচ্ছে Python এর সবচেয়ে বৈচিত্রপূর্ণ ডাটা টাইপ, যেটি থার্ড ব্র্যাকেট [ ] এর ভেতর কমার সাহায্যে উপাদান সহ প্রকাশ করা হয়। List এর উপাদানগুলো একরকম ডাটা টাইপ হবার প্রয়োজন নেই। যেমন,


list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];

 

স্ট্রিং এর মত, লিস্টের মান ০ থেকে শুরু হয়, এবং একে sliced, concatenated করা যায়।

 

Accessing Values in Lists

List এর মান print করতে [ ] ও ইনডেক্স নাম্বার (০, ১ ইত্যাদি) ব্যবহার করা হয়। যেমন,


#!/usr/bin/python

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];

print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]

 

উপরের প্রোগ্রামটির রেসাল্ট হবে,


list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]


 

Updating Lists

একটি list এর এক বা একাধিক উপাদান এর মান নিচের মত আপডেট করা যায়ঃ


#!/usr/bin/python

list = ['physics', 'chemistry', 1997, 2000];

print "Value available at index 2 : "
print list[2]
list[2] = 2001;
print "New value available at index 2 : "
print list[2]

 

এখানে list[2] মান দ্বিতীয় স্টেটমেন্ট এ আপডেট করা হয়েছে। উপরের কোডটি রান করালে নিচের ফলাফল আসবে।


Value available at index 2 :
1997
New value available at index 2 :
2001


 

Delete List Elements

del statement এর সাহায্যে একটি নির্দিষ্ট উপাদান ডিলেট করা যায়, remove() মেথডের সাহায্যে কোন অজানা উপাদান ডিলেট করা যায়, যেমনঃ


#!/usr/bin/python

list1 = ['physics', 'chemistry', 1997, 2000];

print list1
del list1[2];
print "After deleting value at index 2 : "
print list1

 

উপরের প্রোগ্রামটির রেসাল্ট হবে,


['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]

 

Basic List Operations

Python Expression Results Description
len([1, 2, 3]) 3 Length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Concatenation
['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] Repetition
3 in [1, 2, 3] True Membership
for x in [1, 2, 3]: print x, 1 2 3 Iteration

 

Indexing, Slicing, and Matrixes

মনে করি,


L = ['spam', 'Spam', 'SPAM!']

 

 

Python Expression Results Description
L[2] 'SPAM!' Offsets start at zero
L[-2] 'Spam' Negative: count from the right
L[1:] ['Spam', 'SPAM!'] Slicing fetches sections

 

Built-in List Functions & Methods:

Python এ নিচের List Functions গুলো ব্যবহৃত হয়ঃ

SN Function with Description
1 cmp(list1, list2)
Compares elements of both lists.
2 len(list)
Gives the total length of the list.
3 max(list)
Returns item from the list with max value.
4 min(list)
Returns item from the list with min value.
5 list(seq)
Converts a tuple into list.

 

Python নিচের list methods গুলো ব্যবহার করে।

SN Methods with Description
1 list.append(obj)
Appends object obj to list
2 list.count(obj)
Returns count of how many times obj occurs in list
3 list.extend(seq)
Appends the contents of seq to list
4 list.index(obj)
Returns the lowest index in list that obj appears
5 list.insert(index, obj)
Inserts object obj into list at offset index
6 list.pop(obj=list[-1])
Removes and returns last object or obj from list
7 list.remove(obj)
Removes object obj from list
8 list.reverse()
Reverses objects of list in place
9 list.sort([func])
Sorts objects of list, use compare func if given

 

Permanent link to this article: http://bangla.sitestree.com/%e0%a6%aa%e0%a6%be%e0%a6%87%e0%a6%a5%e0%a6%a8-%e0%a6%aa%e0%a7%8d%e0%a6%b0%e0%a7%8b%e0%a6%97%e0%a7%8d%e0%a6%b0%e0%a6%be%e0%a6%ae%e0%a6%bf%e0%a6%82-%e0%a6%a4%e0%a6%be%e0%a6%b2%e0%a6%bf%e0%a6%95/

Leave a Reply