मंगलवार, 12 दिसंबर 2017

Learn Python in Hindi Part 4 (Lists)

आज हम पायथॉन में लिस्ट का उपयोग करने की कुछ और पद्धतीयां सीखेंगे

Finding numbers of items in a list

लिस्ट में कितनी चीजें हैं यह लिस्ट को देखे बिना जानने के लिए हम लेन (len) इस फंक्शन का प्रयोग कर सकते हैं

mylist = ['Dehli', 'Washington', 'Tokyo']
len(mylist)
 3

mylistpair = [('India','Dehli'),('USA','Washington'),('Japan','Tokyo')]
len(mylistpair)
 3


Traversing through the items in a list

लिस्ट के अंतर्गत सभी वस्तुओंको को क्रमशः किस प्रकार से हम प्रयोग में ला सकते हैं यह देखेंगे

mynames = ["Sanjay", "Abhay", "Shailaja", "Vinay", "Ishaan"]
for name in mynames:
    print("Hello",name)

Hello Sanjay
Hello Abhay
Hello Shailaja
Hello Vinay
Hello Ishaan

mynumbers = [11, 22, 33, 44, 55, 66]
for num in mynumbers:
    print("Square of number", num, "is",num*num)

Square of number 11 is 121
Square of number 22 is 484
Square of number 33 is 1089
Square of number 44 is 1936
Square of number 55 is 3025
Square of number 66 is 4356


Using enumerate

लिस्ट के अंतर्गत सभी वस्तुओं के क्रम और उनके नाम किस तरह से हम प्रयोग में ला सकता हैं यह देखेंगे

mylist = ['Dehli', 'Washington', 'Tokyo', 'Mosco', 'London']
for index, name in enumerate(mylist):
    print("The city" , name , "is ", str(index) , " position in  the list")


The city Dehli is  0  position in  the list
The city Washington is  1  position in  the list
The city Tokyo is  2  position in  the list
The city Mosco is  3  position in  the list
The city London is  4  position in  the list

आगे पढ़िए 

Learn Python in Hindi - Part 5 - Lists


कोई टिप्पणी नहीं:

एक टिप्पणी भेजें

टिप्पणी: केवल इस ब्लॉग का सदस्य टिप्पणी भेज सकता है.