Learn Python in marathi Part 5 Lists

आज आपण पायथॉनमध्ये यादी (list)सोबत वापरले जाणारे काही पद्धती methods पाहू.

 पायथॉनच्या यादी  (list) मध्ये समाविष्ट असलेल्या एखाद्या नावाला item जर पहायचे असेल तर त्यासाठी पॉप pop नावाची method पद्धत वापरली जाते

त्यासाठी आपल्याला  यादी  (list) च्या नावासमोर pop लिहून एका कंसात अनुक्रमांक लिहावा लागतो. list मध्ये त्या जागी असलेले item वस्तू/ नाव लिहिलेले आपल्याला दिसते

cities = ["Mumbai", "Pune", "Bangalore", "Calcutta"]
cities.pop(0)
'Mumbai'


जर आपण  pop पद्धति method चा वापर क्रमांक न लिहिता केला तर यादी मधील सर्वात शेवटचे नाव किंवा तेथे जे काही लिहिलेले असेल ते स्क्रीन वर दर्शवले जाते

cities.pop()
'Calcutta'

आपण extend नामक पद्धतीचा उपयोग दोन याद्या  lists जोडण्यासाठी करू शकतो


cities1 = ["Mumbai", "Pune", "Bangalore", "Calcutta"]
cities2 = ["Kolhapur", "Trivendrum", "Belgaon", "Dibrugadh"]
cities1.extend(cities2)
print(cities1)
['Mumbai', 'Pune', 'Bangalore', 'Calcutta', 'Kolhapur', 'Trivendrum', 'Belgaon', 'Dibrugadh']

आपण अधिक चिन्हाचा (+ operator) वापर दोन याद्या lists जोडण्यासाठी करू शकतो

cities1 = ["Mumbai", "Pune", "Bangalore", "Calcutta"]
cities2 = ["Kolhapur", "Trivendrum", "Belgaon", "Dibrugadh"]
cities1 + cities2
['Mumbai', 'Pune', 'Bangalore', 'Calcutta', 'Kolhapur', 'Trivendrum', 'Belgaon', 'Dibrugadh']

यादी  list मध्ये एखाद्या शब्दाचा क्रमांक जाणण्यासाठी आपण index (अनुक्रमांक) या पद्धतीचा method वापर करू शकतो

sweets = ["Rasgulla", "Chamcham", "Basundi", "Balushahi", "Peda"]
sweets.index("Peda")
4

यापुढील लेख

Learn Python in Marathi Part 6 Copy Lists

टिप्पण्या