Chapter 5 : Python Data Structures
Python Language में अच्छी Programming करने के लिए Python Data Structures का ज्ञान होना बहुत महत्वपूर्ण है | Online Payment System, Student Records Management, Database से जुड़े Projects बनाने के लिए Python Language मे Data Structure के Concept का उपयोग किया जाता है | इस Web Page पर Python Data Structure के List, Dictionary , Set जैसे और महत्वपूर्ण Data Structures को सरल भाषा में समझाया गया है , ताकि beginners भी इन्हें आसानी से सीख सकें | इस Chapter को Attempt करने के बाद Site पर दिए गए Practice sets (Problems) को जरूर से Solve करे |
Data Structures क्या होते हैं ?
Data Structure Python Language का एक महत्वपूर्ण Chapter है | Data Structure से मतलब है Data को store , access , delete करना | उदाहरण के लिए , आप 200 किताबें एक कमरे में इधर - उधर फेंक देते है | तो किसी Special book को ढूँढने में घंटों लग जायेगें अगर वही 200 किताबें एक क्रम मे रखी जाए तो किसी Special Book को ढूँढने मे कुछ seconds ही लगेगे | इसी तरह से Python मे Data को Store करने के लिए , Access करने के लिए Data Structure का उपयोग किया जाता है | Python Language मे अच्छी Programming करने लिए बहुत से Data Structures का उपयोग किया जाता है | सभी data Structures का काम एक दूसरे से अलग होता है | नीचे Python मे उपयोग किए जाने वाले महत्वपूर्ण Data Structures को अच्छे से Explain किया गया है |
1. List क्या होती हैं ?
list Python language का सबसे पहला और Basic Data Structure है जिसका उपयोग Python Language मे data को list के Format मे store करने के लिए किया जाता है | list एक Mutable Data Structure है | यानि की List मे Store किए गए data मे changes किए जा सकते है | list मे प्रत्येक element का एक index number होता है | Python मे index number 0 से शुरू होते है | Python Language मे List को create करने के लिए [] Square brackets का उपयोग किया जाता है | नीचे Python Language के अन्दर List से संबंधित एक Python code दिया गया है |
1fruits = ["apple", "banana", "mango"] 2print(fruits)
code Explanation
fruits = > fruits नाम का एक variable create किया गया है जिसमे एक list create की गई
है |
print(fruits) = > ये print() function screen पर fruits variable को display करता है |
List Indexing
Create की गई List के किसी Special Index Number पर Stored Value को प्राप्त करने Indexing का उपयोग किया जाता है | Python मे Indexing 0 से शुरू होती है और Indexing को slicing के नाम से भी जाना जाता है | Python मे list Indexing का एक code नीचे दिया गया है |
1fruits = ["apple", "banana", "mango"] 2print(fruits[0]) 3print(fruits[2])
apple
mango
Code Explanation
fruits = ["apple", "banana", "mango"] = > fruits नाम का एक variable create किया गया है जिसमे
List Data Store किया गया है |
print(fruits[0]) = > ये print() Screen पर List के 0 index number पर stored element को
Show करता है |
print(fruits[2]) = > ये print() Screen पर List के 2 index number पर stored element को
Show करता है |
List Methods
list मे किसी element को जोडना , remove करना , replace करना etc. काम करने के लिए Python कुछ Built-in-Function देता है | Built in Functions वो होते है जो python को install करने के साथ आते है यानि इन को अलग से install करने की ज़रूरत नही होती है | कुछ Built-in-Function List Functions का निम्नलिखित वर्णन किया गया है |
1. remove()
किसी list से किसी element को delete करने के लिए remove function का उपयोग किया जाता है | नीचे एक remove() function का code दिया गया है जिसमे एक list के 0 index number पर stored element को remove किया गया है |
1fruits = ["apple", "mango"] 2fruits.remove(0) 3print(fruits)
['apple']
Code Explanation
fruits = [""] = > fruits एक variable है जिसमे List Data Stored है |
fruits.remove(0) = > fruits variable के 0 index पर stored element को remove किया गया है |
print(fruits) = > ये print() function screen पर list को show करता है |
2. append()
किसी list के end मे किसी element को add करने के लिए append function का उपयोग किया जाता है | append function का नीचे एक code दिया गया है |
1fruits = ["apple" , "mango"] 2fruits.append("Banana") 3print(fruits)
['apple', 'mango', 'Banana']
Code Explanation
fruits = ["apple" , "mango"] = > fruits नाम का एक variable create किया गया है जिसमे
List data store किया गया है |
fruits.append("Banana") = > fruits list के end मे "Banana" fruit name को add किया गया है |
print(fruits) = > ये print() function screen पर list को show करता है |
3.extend()
किसी एक list को दूसरे list से जोडने के लिए extend function का उपयोग जाता है |
1fruits = ["apple"] 2fruits.extend("orange") 3print(fruits)
['apple', 'orange']
Code Explanation
fruits = [""] = > fruits एक variable है जिसमे List Data Stored है |
fruits.extend(orange) = > fruits List मे "orange" Fruit Name add किया गया है |
print(fruits) = > ये print() function screen पर list को show करता है |
4.clear()
list के सभी elements को delete करने के लिए clear() का उपयोग किया जाता है यानि की list को खाली करने के लिए clear() function का उपयोग किया जाता है जैसे :-
1fruits = ["apple", "mango"] 2fruits.clear() 3print(fruits)
Code Explanation
fruits = [""] = > fruits एक variable है जिसमे List Data Stored है |
fruits.clear() = > fruits variable मे stored सारे list elements को delete किया गया है |
print(fruits) = > ये print() function screen पर fruits list को show करता है |
Features Of List
1. Creation = > List को create करने के लिए [] Square brackets का उपयोग किया जाता है |
2. List Operations = > List पर काम करने के लिए List clear , append
जैसे Functions का उपयोग किया जाता है |
3. Mutabel = > Create की गई List मे changes किए जा सकते है |
4. Syntax = > List को create करने के लिए Python मे किसी List Syntax का उपयोग नही किया
जाता है |
2. Tuple
Tuple एक Immutabel Data Structure है मतलब Tuple को एक बार create करने के बाद Tuple मे changes नही किए जा सकते है | Tuple की यही विशेषता Tuple को एक fast और secure data Structure बनाती है | एक से अधिक elements को store करने के लिए Tuple का उपयोग किया जाता है | Tuple मे stored ये elements Int , Float , String जैसी किसी भी type की values हो सकती है | नीचे एक Tuple को create करने का Python Code दिया गया है |
1tp = ("apple", 2, 5) 2print(tp)
apple
2
5
code Explanation
tp = > tp नाम का एक variable create किया गया है जिसमे Tuple का data store किया गया है |
print(tp) = > ये print() function screen पर tp variable को show करता है |
Tuple Indexing
किसी Tuple के किसी Index Number पर क्या element store है पता करने को Tuple Indexing कहा जाता है | Indexing 0 से शुरू होती है | नीचे Tuple Indexing का एक Python Code दिया गया है |
1tp = ("apple", 2, 5) 2print(tp[1]) 3print(tp)
2 ['apple', 2, 5]
tp = ("apple", 2, 5) = > tp नाम का एक variable create किया गया है जिसमे tuple values stored है |
print(tp[1]) = > ये print() function screen पर tp variable के index 1 पर stored value को show करता
है |
print(tp) = > ये print() screen tp variable मे stored सभी values को show करता है |
Tuple Methods
एक Tuple को दूसरे Tuple से जोडना , Tuple को repeat करना , Tuple को delete करना आदि Operations Tuple पर Perform करने के लिए Python मे Tuple Methods का उपयोग किया जाता है | Python मे Tuple के उपयोग किए जाने वाले कुछ महत्वपूर्ण Methods का निम्नलिखित वर्णन किया गया है |
1. Repeatation
एक Tuple को एक से अधिक बार repeat करने के लिए Python मे Repeatation Method का उपयोग किया जाता है | Repeatation Method के लिए (*) Operator का उपयोग किया जाता है | नीचे Repeatation Method का एक Python Code दिया गया है |
1tp = ("apple", 2 ,5 ) 2print(tp * 2)
apple 2, 5 apple 2
Code Explanation
tp = ("apple", 2 ,5 ) = > tp नाम का एक variable create किया गया है जिसमे Tuple values store
की गई है |
print(tp * 2) = > ये print() function screen पर tp variable मे stored values को दो बार display
करता है |
2. Membership Methods
Membership Method Tuple का एक famous और बहुत महत्वपूर्ण Method है | वो ऐसे कि यदि आपको एक Tuple दिया जाए और Tuple मे Special Value को पता करने के लिए कहा जाए तो आपको वो Special value पता करने मे बहुत Time लग जाएगा , लेकिन Membership Method के उपयोग से कोई Tuple मे Special value check करपाना बहुत सरल है | Membership method का उपयोग करने के लिए in और not Operator का उपयोग किया जाता है | नीचे Membership Method का एक Code दिया गया है |
1tp = ("apple", 2 ,5) 2print(2 in tp)
True
Code Explanation
tp = ("apple", 2 ,5) = > tp नाम का एक variable create किया गया है जिसमे Tuple values store
की गई है |
print(2 in tp) = > ये print() function screen पर 2 value tp variable मे है या नही result show करता है
|
3. Slicing
Tuple के किसी part को access करने के लिए slicing method का उपयोग किया जाता है | start और end दो Slicing के महत्वपूर्ण Parts है | start मतलब जहा से elements को index किया जाना है और end मतलब जहा slicing को stop किया जाना है | नीचे Slicing Method का एक Python Code दिया गया है |
1tp = ("apple", 2 ,5) 2print(tp[0 : 2])
apple
2
5
Code Explanation
tp = ("apple", 2 ,5) = > tp एक variable है जिसमे tuple values store की गई है |
print(tp[0 : 2]) = > ये print() function screen पर tp variable मे stored 0 index से 2 Index
तक के elements को show करता है |
Features Of Tuple
1. Creation = > Python Language मे Tuple को create करने के लिए () parentheses brackets का उपयोग किया
जाता है |
2. Tuple Operations = > Tuple पर काम करने के लिए Tuple clear , Tuple Slicing
जैसे Methods का उपयोग किया जाता है |
3. Immutabel = > Tuple एक Immutabel Data Structure है जिसमे changes नही किए जा सकते है |
4. Use = > Python मे Tuple Slicing के लिए in और and Operators का उपयोग किया जाता है |
3. Dictionary
Python मे Dictionary एक Mutabel Data Structure है जिसका उपयोग data को store करने के लिए किया जाता है | Dictionary मे data को key और value के रूप मे store किया जाता है | मतलब Dictionary मे हर element की एक key होती है , और हर key की एक unique value होती है | Dictionary का उपयोग json की तरह ही किया जाता है | Dictionary Python का एक fast और advanced data Structure है | Dictionary को Curly{} brackets मे create किया जाता है | नीचे एक Dictionary create की गयी है |
1dict = { 2 key1 : value1 , 3 key2 : value2 4 }
Syntax Explanation
dict : dict नाम का एक variable create किया है | र
{ } : Dictionary को create करने के लिए Curly brackets का उपयोग किया गया है |
key1 : एक key create की गई है |
value1 : key की value दि गई है |
: : key और value को अलग करने के लिए : का उपयोग किया गया है |
Dictionary Methods
एक Dictionary से key को access करना , remove करना etc. काम करने के लिए कुछ Methods है | किसी Dictionary को बनाने से लेकर access करने तक ये Methods बहुत उपयोगी है | कुछ famous Dictionary methods का निम्नलिखित वर्णन किया गया है |
1. remove method
Dictionary एक mutable data Structure होने के कारण Dictionary मे changes किए जा सकते है | remove एक method है जिसके उपयोग से एक Dictionary मे से किसी item , key या value को remove किया जा सकता है | जैसे :-
1course-details = { 2 "course-name" : "python" , 3 "fees" : 0 4 } 5del course-details["fees"] 6print(course-details)
Code Explanation
course-details = > course-details नाम का एक variable create किया है |
{ } = > Dictionary को create करने के लिए Curly brackets का उपयोग किया गया है |
"course-name" = > course-name नाम की एक key create की गई है |
"python" = > key की value दि गई है |
: = > key और value को अलग करने के लिए : का उपयोग किया गया है |
del course-details["fees"] = > course-details नाम के variable मे से fees
नाम की key को remove किया गया है |
2. add method
Dictionary मे किसी नए key और value के pair को add करने के लिए add method का उपयोग किया जाता है | add method का एक code निम्नलिखित दिया गया है |
1course-details = { 2 "course-name" : "python" , 3 "fees" : 0 4 } 5course-details["duration"] = "3months" 6print(course-details)
Code Explanations
course-details["duration"] = "3months" = > course-details नाम के variable मे एक duration नाम की add की गई है और key की value देने के लिए assignment Operator(=) का use किया गया है |
3. value access method
किसी Dictionary की key की value को access करने के लिए इस method का उपयोग किया जाता है | इसके लिए [ ] Square brackets का उपयोग किया जाता है | जैसे :-
1course-details = { 2 "course-name" : "python" , 3 "fees" : 0 4 } 5print(course-details["fees"])
Code Explanation
print(course-details["fees"]) = > course-details से fees key की value को print की गई है |
Features of Dictionary
1. Creation = > Python मे Dictionary को create करने के लिए { } brackets का उपयोग किया जाता है |
2. Data Store = > Dictionary मे data key और Value के pair मे store किया जाता है |
3. Access = > Dictionary एक mutable Data Structure है , इसलिए इसमे stored data को access
करपाना सरल है |
4. Indentation = > Dictionary मे Indentation होती है , इसलिए Dictionary से Programs समझने
मे सरल होते है |
4. Sets
Set एक Mutable data Structure है जो Python का intresting data Structure है वो ऐसे क्योंकि Set एक Mutable Data Structure होने पर भी Set मे Duplicate data Store नही कि जा सकता है | Set को create करने के लिए { } (curly braces) का उपयोग किया जाता है , या set() function का उपयोग करके भी Set को create किया जा सकता है | नीचे दिए गए Code Editor मे एक Set Create किया गया है |
1st = {4 , 1 , 6 , 7} 2print(st)
{1, 4, 6, 7}
Code Explanation
st = {4 , 1 , 6 , 7} = > st नाम का एक variable create किया गया है जिसमे एक Set की values
store की गई है |
print(st)= > ये print() function screen पर st variable मे stored values को show करता है |
Features Of Set
1. Creation = > Python मे set को create करने के लिए {} Curly braces का उपयोग किया जाता है |
2. Indexing Method = > क्योंकि Set मे stored data sequence मे नही होता है , इसलिए Set पर Indexing
method काम नही करता है |
3. Immutable Elements = > Set के अंदर केवल immutable data types (जैसे number, string, tuple) ही रखे
जा सकते है |
4. Mutable = > Set एक Mutable Data Structure है , इसलिए Set मे stored data को access किया जा सकता है
|
5. Set() = > Set मे data को केवल {} braces से ही नही Set() function से भी store किया जा सकता है |
Difference Between List , Tuple , Dictionary , Set
| Base | List | Tuple | Dictionary | Sets |
|---|---|---|---|---|
| 1. Speed | Midium | Fast | Fast | Fast |
| 2. Duplicate Data | Yes | Yes | No | No |
| 3. Mutabel | yes | No | Yes | Yes |
| 4. Data sequence | Yes | No | No | No |
| 5. Immutabel | No | Yes | No | NO |
Advantages Of Data Structures
1. Organized Data = > Data Structures Data को सही Order और sequence मे रखते है जिसके कारण
data को access करना और data पर काम सरल होता है |
2. Fast Access = > क्योंकि data सही Order मे stored होता , इसलिए Data को Access करपाना सरल
होता है |
3. Reusability = > एक बार बनाए गए data structure logic को कई जगह reuse किया जा सकता है |
4. Computer Memory = > List , Tuple जैसे Data Structures Memory का उपयोग बहुत कम करते है जिसके
कारण Computer की Performance अच्छी होती है |
5. Debuging = > क्योंकि Data Structures के उपयोग से बडे - बडे Programs छोटे - छोटे Parts मे devide
हो जाते है , इसलिए Programs मे आई हुई Errors को पता करपाना सरल हो जाता है |
You have completed This Chapter ! 🎉
Now you can choose any one of the following options to test your knowledge :
Help Center
Users and we all help you together .