Type-Specific Operations in Python

Type-Specific Operations in Python – Python के List को हम अन्‍य Programming Langauge के Array की तरह समझ सकते हैं लेकिन Python के List में हम Different Data Types के Data Items को Store कर सकते हैं, जबकि Array में Basically केवल एक ही Data Type के Data को Store किया जा सकता है।

साथ ही List Object में हम जितने चाहें उतने Data Items Add कर सकते हैं, और प्रत्‍येक Data Items को Store करने के लिए जरूरी Space Python Interpreter स्‍वयं ही Dynamically Allocate करता रहता है और किसी Item को Delete करते ही उस Item के लिए Allocated Space, Python Interpreter द्वारा स्‍वयं ही Free कर दिया जाता है।

यानी Python का List Data Structure, जरूरत के अनुसार Dynamically Grow व Shrink हो सकता है जबकि Array एक Fixed Sized Data Structure है, इसलिए Array Define करते समय ही हमें उसकी Size को Fixed कर देना जरूरी होता है। List की On Demand Grow व Shrink होने की प्रक्रिया को हम निम्‍न Example द्वारा आसानी से समझ सकते हैं-

[code]
FileName: ListGrowShrink.py
lst = ["KRISHN", "MURARI"]

print("Names in the List:\n {}".format(lst))

newName = input("\nInput New Name of Lord Krishna: ")
lst.append(newName)
print("\nNames in the List:\n {}".format(lst))

delName = input("\nInput Index Number to Delete a Name: ")
lst.pop(int(delName))
print("\nNames in the List:\n {}".format(lst))

Output
Names in the List:
 ['KRISHN', 'MURARI']

Input New Name of Lord Krishna: TRIPURARI
Names in the List:
 ['KRISHN', 'MURARI', 'TRIPURARI']

Input Index Number to Delete a Name: 1
Names in the List:
 ['KRISHN', 'TRIPURARI']
[/code]

इस Example में हमने List lst के साथ append() Method का प्रयोग करके User द्वारा Input के रूप में लिए गए एक नाम को List lst के अन्‍त में Append किया है और फिर User द्वारा Input किए गए Index Number की Location के नाम को List lst से pop() Method का प्रयोग करते हुए Delete कर दिया है।

append() Method, List lst के अन्‍त में एक नया Item Append करके List की Length को Grow करते हुए 2 से 3 कर देता है, जबकि pop() Method इसी List lst के Index Number 1 पर Exist Item को Delete करके List की Length को Shrink करते हुए फिर से 2 कर देता है।

append() Method हमेंशा List के अन्‍त में ही नया Item Append करता है, जबकि यदि हमें कि किसी Specific Index Location पर किसी Item को Insert करना हो, तो इस जरूरत को पूरा करने के लिए हमें List के insert() Method को Use करना पड़ता है।

जबकि List के किसी Item को उसके Index Number के स्‍थान पर उसकी Value के माध्‍यम से Delete करना हो, तो उस स्थिति में हमें pop() Method के स्‍थान पर remove() Method को Use करना पड़ता है और इस remove() Method में Parameter के रूप में वह Data Value Pass करते हैं, जिससे Match करते हुए List Item को Delete किया जाना होता है।

इसी तरह से जब हमें किसी List में Multiple Items को List के अन्‍त में एक साथ Add करना होता है, तब हम extend() Method का प्रयोग करते हैं।

List हमें ये सुविधा भी देता है कि हम इसके अन्‍दर Stored Data Items की Sorting भी कर सकते हैं और List के Data Items को Reverse Order में भी Set कर सकते हैं। Sorting करने के लिए हमें sort() Method Use करना पड़ता है, जबकि reserse() Method का प्रयोग करके हम List के Items को Reverse Order में Arrange कर सकते हैं। जैसे-

[code]
FileName: SortAndReverseMethods.py
lst = ["KRISHN", "MURARI", "TRIPURARI", "BIHARI"]
print("Names in the List:\n {}".format(lst))

print("\nSorted List: ")
lst.sort()
print("\nNames in the List:\n {}".format(lst))

print("\nReversed List: ")
lst.reverse()
print("\nNames in the List:\n {}".format(lst))

Output
Names in the List:
 ['KRISHN', 'MURARI', 'TRIPURARI', 'BIHARI']

Sorted List:
Names in the List:
 ['BIHARI', 'KRISHN', 'MURARI', 'TRIPURARI']

Reversed List:
Names in the List:
 ['TRIPURARI', 'MURARI', 'KRISHN', 'BIHARI']
[/code]

Python in Hindi - BccFalna.comये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook Python in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी है, तो निश्चित रूप से ये EBook भी आपके लिए काफी उपयोगी साबित होगी।

Python in Hindi | Page: 602 | Format: PDF

BUY NOW GET DEMO REVIEWS