Seekho Coding

Data Structures

Data Structure एक ऐसी तकनीक है जिसके उपयोग से डेटा को संग्रहित (Store) , संगठित (Organize) और प्रबंधित (Manage) किया जाता हैं। हर Program में Data store किया जाता है। अगर Data को सही तरीके से store नहीं किया जायेगा तो: memory ज़्यादा खर्च होगी डेटा ढूँढने में समय लगेगा program slow चलेगा etc. इसीलिए डेटा को सही तरीके से store , manage और Organize करने के Process को Data Structure कहते हैं |
उदाहरण के लिए =>
आप 200 किताबें एक कमरे में इधर-उधर फेंक देते है | तो किसी स्पेशल book को ढूँढने में घंटों लग जायेगें। अगर वही 200 किताबें अगर क्रम मे रखें तब कुछ seconds ही लगेगें।
इस पाठ में आप चार मुख्य Data Structures को सीखेंगे | List, Tuple, Dictionary और Set इन data Structure को सीखने के साथ - साथ आप इनके Introduction , advantages और Indexing भी समझेंगे। Python मे इन चारो data Structure का बहुत उपयोग किया जाता है |:

(1.List)

list python language का सबसे पहला और basic data Structure है | जिसका उपयोग कर पाना बहुत सरल है | list मे प्रत्येक element का एक index number होता है | Python मे index number 0 से शुरू होते है | list को create करने के लिए [] Square brackets का उपयोग किया जाता है | list एक mutable होती है | यानी create की गई list के अन्दर store data मे बदलाव (change) किये जा सकते है | python programming के अन्दर lists कुछ इस प्रकार create की जाती है |

1fruits = ["apple", "banana", "mango"]
2print(fruits)

code Explanation

fruits नाम का एक variable create किया गया है | जिसमे एक list create की गई है | list को create करने के Square brackets का उपयोग किया जाता है |

List Indexing

किसी spacial number पर कैसी या क्या value है , को प्राप्त करने को Indexing कहा जाता है | Python मे Indexing 0 से शुरू होती है | और Indexing को slicing के नाम से भी जाना जाता है | Python मे list Indexing का एक उदाहरण नीचे दिया गया है |

1fruits = ["apple", "banana", "mango"]
2print(fruits[0])
3print(fruits[2])
        
  • Output
  •     apple
        mango
                    

    Code Explanation

    print(fruits[0]) print function से fruits नाम का variable print किया गया है element को अलग करने के लिए comma(,) का उपयोग किया गया है | [0] का मतलब है कि fruits के 0 index के item को print करो |

    list Method

    list मे किसी element को जोडना , remove करना , replace करना etc. काम करने के लिए Python कुछ Built-in-Function देता है | Built in Functions वो होते है जो python को install करने के साथ आते है यानि इन को अलग से install करने की ज़रूरत नही होती है | कु छ List Functions का निम्नलिखित वर्णन किया गया है |

    1. remove()

    किसी list से किसी element को delete करने के लिए remove function का उपयोग किया जाता है | एक list से एक element या एक से अधिक element को remove का एक example नीचे दिया गया है |

    1fruits = ["apple", "mango"]
    2fruits.remove(0)
    3print(fruits)
            
  • Output
  •             mango
                    

    2. append()

    किसी list के end मे किसी element को add करने के लिए append function का उपयोग किया जाता है | append function का नीचे एक example दिया गया है |

    3.extend()

    किसी एक list को दूसरे list से जोडने के लिए extend function का उपयोग जाता है |

    1m_fruits = ["apple"]
    2fruits.extend("orange")
    3print(fruits)
            
  • Output
  •     apple
        orange
                    

    4.clear()

    list के सभी elements delete करने के लिए clear() का उपयोग किया जाता है | जैसे :-

    1fruits = ["apple", "mango"]
    2fruits.clear()
    3print(fruits)
            

    (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 को parentheses() brackets मे create किया जाता है | python programming के अन्दर Tuple कुछ इस प्रकार create किया जाता है |

    1tp = ["apple", 2 ,5 ]
    2print(tp)
            
  • Output
  •             apple
                2
                5
                    

    code Explanation

    tp नाम का एक variable create किया गया है | जिसमे एक Tuple create की गया है | Tuple को create करने के parentElement brackets का उपयोग किया जाता है |

    Tuple Indexing

    एक Tuple मे किसी spacial number पर कैसी या क्या value है , को देखने के लिए Indexing का उपयोग किया जाता है | Python मे Indexing 0 से शुरू होती है | और Indexing को slicing के नाम से भी जाना जाता है | Python मे Tuple Indexing का एक उदाहरण नीचे दिया गया है |

    1tp = ["apple", 2 ,5 ]
    2print(tp[1])
    3print(tp)
    
            
  • Output
  •      apple
                    

    Tuple Methods

    एक Tuple को दूसरे Tuple से जोडना , Tuple को repeat करना etc. operations करने के लिए Python कुछ Methods देता है | और इन Methods को follow करते हुए , Tuple का use करना बहुत सरल है | Python मे Tuple के कुछ Methods का निम्नलिखित वर्णन किया गया है |

    1. Repeatation

    एक Tuple को एक से अधिक बार repeat करने के लिए Python मे Repeatation Method का उपयोग किया जाता है | Repeatation Method के लिए (*) का उपयोग किया जाता है | Repeatation function का एक उदाहरण नीचे दिया गया है |

    1tp = ["apple", 2 ,5 ]
    2print(tp * 2)
    
            
  • Output
  •      apple 2, 5 apple 2
                    

    2. Membership Methods

    अब कोई element Tuple मे है या नही ये पता करने के लिए Membership Method का उपयोग किया जाता है | Membership method के उपयोग के लिए in और not का उपयोग किया जाता है | और output True or False मे आता है जैसे :-

    1tp = ["apple", 2 ,5 ]
    2print(2 in tp)
    3
    4print(5 not in tp)
            
  • Output
  •         True
            True
                    

    3. Slicing

    Tuple के किसी part को access करने के लिए slicing का उपयोग किया जाता है | slicing के 2 मैन part होते 1. start 2. end है | start जहा से elements को index किया जाना है और end मतलब जहा slicing को stop किया जाना है | जैसे :-

    1tp = ["apple", 2 ,5 ]
    2print(tp[0 : 2])
            
  • Output
  •         apple 
            2
            5
                    

    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 करने Syntax निम्नलिखित दिया गया है |

    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 Method

    एक 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)
            

    Syntax Explanation

    course-details नाम का एक variable create किया है | फिर
    { } Dictionary को create करने के लिए Curly brackets का उपयोग किया गया है | फिर
    "course-name" नाम एक key create की गई है | character को double quotes("") मे यानि String के रूप मे store किया जाता है | फिर
    "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 का एक example निम्नलिखित दिया गया है |

    1course-details = {
    2    "course-name" : "python" ,
    3    "fees" : 0
    4  }
    5course-details["duration"] = "3months"
    6print(course-details)
            

    Syntax explanations

    course-details["duration"] = "3months"लिखा गया है कि course-details नाम के variable मे एक duration नाम की add करो और key की value देने के लिए assignment Operator(=) का use किया गया है |

    3. value access method

    किसी की value को access करने के लिए इस method का उपयोग किया जाता है | इसके लिए [ ] Square brackets का उपयोग किया जाता है | जैसे :-

    1course-details = {
    2    "course-name" : "python" ,
    3    "fees" : 0
    4  }
    5print(course-details["fees"])
            

    Syntax Explanation

    print(course-details["fees"]) का मतलब है कि course-details से fees key की value को print करो |

    (3.Sets)


    set एक mutable data Structure होता है | जि स का उपयोग Curly brackets {} का होता है। Set unique elements का unordered collection होता है। और set Duplicate values नहीं रखता। एक set कुछ इस प्रकार create होता है |

    1st = {4 , 1 , 6 , 7}
    2print(st)
    
            

    Syntax Explanation

    print(st) करने पर st नाम के variable मे stored सभी value print होगी |

    Difference Between List , Tuple , Dictionary , Set

    Base List Tuple Dictionary Sets
    Speed Midium Fast Fast Fast
    Duplicate Yes Yes No No
    Mutabel yes No Yes Yes
    Ordered Yes No No No
    Immutabel No Yes No NO

    अध्याय समाप्त — आगे बढ़ें

    आपने इस chapter मे python के बहुत ही महत्वपूर्ण concepts समझे है जो आगे की प्रोग्रामिंग सीखने मे बहुत Helpful हैं। आगे के chapter मे आप python के और महत्वपूर्ण concepts सिखेगे जैसे :- functions आगे के chapter की और बढ़ने के लिए नीचे दिए गए बटन पर क्लिक करे |

    Next Chapter