This Website for Those Students who are searching for a platform to study online in their own Language Hindi, on this website they will learn all courses like IAS, programming, web designing, data science, and competitive exam preparation, etc.


Python - List Data Type

Python - List Data Type

Python - List Data Type





आज हम इस पोस्ट में python data types List के बारें में पढेंगे तो चलिए शुरू करते है.



Python में List एक Built in data type में से एक है जिसका Use Multiple Data Type की Value को जैसे Intiger, Float, Complex और String के जैसी Value हम Sequence मे Store कर सकते है ये वैसे सा ही है जैसे Single Variable मे Multiple Value.

उसको हम एक Example की मदत से समझने की कोसिस करते है जैसे हमे एक एक ऐसा Variable बनाना है जो multiple Value hold करे जैसे मेने बोला आप एक Fruite नाम का Variable बनाइये जो Fruites की नाम hold करे अब Fruites तो bahut है जैसे Mango ,banana और papaya आदि

अब इन सबको एक Variable मे रखना है अगर आप String type के Variable Create करके करते हो तो आप एक String के रूप मे सारे नाम आएंगे वो अलग - अलग value नहीं होंगे वो पूरी एक String कह लायेंगी लेकिन हमे तो अलग - अलग value चाइये ताकी जिसे चाहे उसे use कर सके तो अब हमे ऐसी Data Type की जरूरत है जो Single Variable मे Multiple वैल्यू Hold कर सके तो यहाँ List Data Type का Concepte काम आता है

जरूरी नहीं हम List Data Type मे String type की Value hold कर सकते ऐसा नहीं है हम जैसा ऊपर बताया गया है मे Intiger, Float, Complex और String के जैसी Value हम Sequence मे Store कर सकते है. आगे हम Example की मदत से मझेंगे।

तो चलिए अब कुछ Basic Operation और Methods के बारे में जान लेते जैसे List को कैसे Create ,Delete और इन पर कैसे-कैसे Operation Perform किये जा सकते है।



Creating List



Python में List को Create करने के लिए सबसे पहले Variable काम नाम लिखते है फिर Assign Operator का Use करते हुए हम Square Brackets [] में Comma (,) separator के सात value देते है जैसे आप Example में देख सकते हो।

  
  
  
      Exampe : fruites = ["banana","apple","orange"] 
      
      

  


Note: धयान रहे अगर आप list Data Type String Use करते हो तो String को आपको Single Quote या Dubble Quote मे लिखे और अगर Intiger Value है तो बिना Single Quote और Dubble Quote के दे सकते हो।

  
    
      For Exampe : RealNumbers = [1,2,3,4,5,6,7,8,9] 
      
      

  



  
    
    list1 = [1, 2, 3, 4, 5] #Integer Tuple
    
    list2 = [1.5, 2.4, 3.8, 4.4, 5.7] #Float Tuple
    
    list3 = [1, 2, "Hello", 4.5] #Mixed Data Types Tuple
    
    list4 = [1, 2, [1, 2], 4.5] #Nested Tuple


  

list Indexing



हर Programming Language में indexing की शुरुआत '0' से होती है उसी तरह से Python में भी List के stating item का index '0' से शुरू होता है |



  

    
    strTuple = ["H", "e", "l", "l", "o"]



ItemsNumber item1 item2 item3 item4 item5
Index 0 1 2 3 4
Items "H" "e" "l" "l" "o"


Negative Indexing



Python में List के आखिरी index '-1' से शुरू होता है | जिसकी मदत से हम Last के item को Access कर सकते है.



  
    
    greeting  = ["H", "e", "l", "l", "o"]


  


ItemsNumber item1 item2 item3 item4 item5
Index -5 -4 -3 -2 -1
Items "H" "e" "l" "l" "o"


Accesing List



index की मदत से हम List के items को access किया जा सकता है |



Syntax :

  
    
    list[index_value]


  


Code :

  
    list = [1, 2, "H", 2.8]
    
    print(list[0])
    
    print(list[1])
    
    print(list[2])
    
    print(list[3])
 


Output :

  
    1

    2
    
    H
    
    2.8


  


Invalid Indexing



अगर हम invalid index करते है मतलब वो item access करना चाहते है जो List मे नहीं है तो 'indexError' का exception आ जाता है |



Source Code :

  list = [1, 2, "H", 2.8]
print(list[10])



Output :

   
    
    print(list[10])

    IndexError: list index out of range


  


Accessing Nested List

अगर हमे Nested List या Multidimensions List को Access करना चाहते है तो Row और Coloum की मदत से हम Access कर सकते जिसका Example निचे दिया गया है।



  
  
    list = [1, 2, "Hello", ["R", "U"]]
    
    print(list[3][0]) #Output : R
    
    print(list[3][1]) #Output : U


  


list item Reassign



list को change कर सकते है और उसे re-assign किया जा सकता है |



  
  list = [1, 2, 3, 4, 5]
  
  print(list)
  
  #Output : [1, 2, 3, 4, 5]
  
  #changing item
  
  list[0]=9
  
  #after changing
  
  print(list)
  
  #Output : [9, 2, 3, 4, 5]
  
  
  
  
  #re-assign list
  
  list = [6, 7, 8, 9, 10]
  
  print(list)
  
  #Output : [6, 7, 8, 9, 10]


  


Deleting list



List को delete करने के लिए 'del' Operator का उपयोग किया जा सकता है पर याद रखे 'del' Operator से पूरी list भी delete किया जा सकता है और list के एक-एक item को भी delete किया जा सकता |



  
  
  list = [1, 2, 3, 4, 5]
  
  del list[0]
  
  #Output : [2, 3, 4, 5]
   

  del list

  print(list)
 
#Output : Error no list exits 
 

  


Slicing Of List



यहाँ Slicing का यहाँ ये मतलब है अगर हमे list का कोई भाग या perticular कोई Sub List चाइये है तो हम Slicing की मदत से कर सकते जैसे की आप निचे Syntax और Example मे देख सकते हो।

colon(:) के left में sub-List कहा से start करना है और colon(:) के right में कहा पर end करना है वो दिया जाता है |

Slicing के लिए colon को 'slicing operator' कहा जाता है |

Syntax :

  tuple(start:end)

  
Source Code :
  
    list = [1, 2, "H", 2.8]

    print(list[0:1]) #output : [1]

    print(list[0:2]) #output : [1, 2]

    print(list[1:3]) #output : [2, 'H', 2.8]

    print(list[3:1]) #output :[]
 
Output :
  
    [1]

    [1, 2]

    [2, 'H', 2.8]

    []
    

  


अगर colon(:) के left side का index invalid होता है तो blank list[[]] return होती है और colon(:) के right side का index invalid होता है तो Tuple के left index से आखिरी तक index return होता है | अगर दोनों ही invalid index होता है तो blank tuple(()) return होता है |

For Example,

  
    list = [1, 2, "H", 2.8]

    print(list[0:1]) #Output  [1]

    print(list[0:2]) #Output [1, 2]

    print(list[1:3]) #Output [2, 'H']

    print(list[1:15]) #Output [2, 'H', 2.8]

    print(list[10:4]) #Output []

  



List Concatenation

अगर आपको 2 या ज्यादा List का Concatenation यानि जोड़ना है 2 List को Add करना होतो आप (+) Operator का use कर सकते है निचे Example की मदत से समझाया गया है।



  
    tuple = [1, 2, "H", 2.8] + ["Hello", [2, 8]]

    print (tuple)


    #Output : [1, 2, 'H', 2.8, 'Hello', [2, 8]]

  


Iterating of List

दोस्तों अगर आपको List के सारे Element print करने होतो हम loop की मदत से कर सकते है जैसा की निचे Example में बताया गया है जरुरी नहीं की आप Iterating की मदत से print ही करे इसका Use कर के आप सभी Operation perfome कर सकते है जैसे Addition , Multiplication ,in and not-in operators आदि।



  

  tuple = (1, 2, "H", 2.8)

  for i in tuple:
     print(i)

 #Output :
 #1
 #2
 #H
 #2.8


  


Operations on List



दोस्तों List पर हम अलग – अलग Operators का प्रयोग कर सकते है |

जैसे addition, multiplication, in and not-in operators.

तो चलिए अब एक - एक करके देखते है ।



Addition Operator

तो अब देखते है की कैसे हम 2 या 2 से ज्यादा Tuples को Addition कर सकते है।



        
        
       a=[1,2,3]

       b=[4,5,6]

       a+b
       
      #output [1, 2, 3, 4, 5, 6]
      
  


Multiplication Operator

तो अब देखते है की कैसे हम Tuples का Multiplication कर सकते है।



        
        
       a=[1,2,3]

       b=[4,5,6]
       
       a*2
       
      #output  [1, 2, 3, 1, 2, 3]
      
          
 


In Operator

दोस्तों In Operator का Use हम जब करते है जब हमे पता करना हो की कोई Item List में है या नहीं अगर वो item List मे True return करता है तो हम वह In Operator कर सकते है।



             
 
       3 in a
      #output  True
       4 in a
      #output   False
      
          
 


Not-In Operator

जैसे हमने जाना In Operator क्या करता है वैसे ही Not-In Operator भी True और False return करता है बस फर्क इतना है की ये जब true retrun करता है जब कोई item List में नहीं होता नहीं तो false retrun करता है,

तो इस Operator Use हम जब करते है जब हमे cheak करना हो की वो item List मे है या नहीं .



                   
     
       3 not in b
      #output True
      
       4 not in b
      #output  False

    
 


List Related built-in Functions

List पर काम करने वाले कुछ Functions को अब हम उदाहरणों के साथ देखेंगे | यह कुछ Functions है – len(), sum(), min(), max() |

इन Functions में से sum(), min(), max() Function List पर तभी काम करेंगे जब List के सारे Elements Numeric data type (int, float) के हो |



len()



हम len() Function की मदद से हम List में कितने item है ये जान सकते है.



        

            
       text=["I", "Love","My","India"]
       type(text)
       
      <class 'List' >
       len(text)
      4





जैसाकि हम इस Example में देख सकते है | यहां एक text List है, इसमें चार item है | function len () की मदत से हम List की Length पता कर सकते है |



sum()



sum() Function की मदत से हम List के item का sum कर सकते है जैसा की निचे Example की मदत से बताया गया है .



        

          data=[1,20,3.14,30]
          
          print[sum[data]]
          
          #output  54.14
          
        



दोस्तों मे आपको बता दू की ऐसा करने के लिए, List के सारे element Category (int, float) के होने चाहिए |

नही तो Error आएगा जैसा की आप निचे देख सकते है |



          

            data=[1, 3.14,"Hello"]
       
           
            print[sum[data]]

          
        


Output
              

           Traceback (most recent call last):
             File "<pyshell#8>", line 1, in<module>
               sum(data2)
           TypeError: unsupported operand type(s) for +: 'float' and 'str'
  


min()



इस min Function से हम यह जान सकते है की List मे के सबसे छोटा item कौन सा है |



  

    data=[10,20,30,40,50]


    print[min[data]]

    #output 10




जब List के Element अलग – अलग प्रकार के हो तो हमे ये Error मिलेगा.



        

          
          data=[10,20,30.45,"Hello"]
          
          min(data)
          
           


Output

        

          
      Traceback (most recent call last):
        File "< pyshell#20 >", line 1, in <module>
          min(data)
      TypeError: '<' not supported between instances of 'str' and 'int'


 


min Function हमे Error देगा | क्योंकि data के अंदर एक Element String Category का है |

इसलिए min Function List पर काम नहीं कर सकता |



max()



Tuple की सबसे बड़ी value को ढूंडने के लिए हमे max() Function इस्तेमाल करना होगा |

निचे दिए उदाहरण से हम max() Function कैसे काम करता है ? यह देख सकते है |



      
      
        data=[10,20,30,40,50]
 
        max(data)

       #output 50



List Functions



List एक mutable data type होने के कारण हम इसके Element को Update या Delete कर सकते |

List Data Type मे count() और index() का use कर सकते लेकिन Tuple में हम इनका use नहीं कर सकते |



Count

यह Function कौन सा Element List में कितनी बार आया है , यह बताता है |



        

          animals=["Dog","Cat","Dog","Monkey","Dog"]
          
        
          animals.count["Dog"]
          
         #output 3 
         
          animals.count["Cat"]
          
        #output  1



Previous
Next Post »

Quote

"Educationists should build the capacities of the spirit of inquiry, creativity, entrepreneurial and moral leadership among students and become their role model."

Dr. Abdual Kalam