यहाँ हम एक बात जान लें की पायथॉन में हम एक वाक्य में हरेक अक्षर को उसके स्थान से पढ़, हटा या बदल सकते हैं. उसके स्थान को इंडेक्स कहते हैं. यह शून्य से गिनना शुरू होता है. उदहारण के लिए
Saying = "A Friend in need in a Friend indeed"
print(Saying[0], Saying[3], Saying[4],Saying[7])
A r i d
यही विधि अन्य चीजों जैसे, शब्द, अंक या कोई और वाक्य जो एक लिस्ट में लिखा हो, का स्थान गिनने में उपयोग की जाती है
Lists
पायथॉन के लिस्ट में आप नाम, अंक या फोर्मुले भी लिख सकते हो. कुछ उदहारण नीचे दिए गए हैं
stateCapitals = ["Mumbai", "Delhi", "Calcutta", "Hyderabad","Bengluru"]
print("Capital of Maharashtra is "+ stateCapitals[0])
Capital of Maharashtra is Mumbai
myNumbers=[15,54,87,99,67]
print("The Fourth number in the list is" , myNumbers[3])
99
पायथॉन की खाली (empty) लिस्ट भी बनाई जा सकती है जिसमे बाद में यूजर के द्वारा चीजें जोड़ी जा सकती हैं. लिस्ट के भीतर आप लिस्ट (नेस्टेड लिस्ट) बना सकते हो. एक लिस्ट से आप चीजों को हटा, या जोड सकते हो. दो लिस्टों को जोड़ कर एक लम्बी लिस्ट भी बनाई जा सकती है.
stateCapitals.append("Ahmedabad")
print("The capital of Gujarat used to be", stateCapitals[5])
The capital of Gujarat used to be Ahmedabad
stateCapitals.insert(5,"Gandhinagar")
print("However, the present capital of Gujarat is ", stateCapitals[5])
However, the present capital of Gujarat is Gandhinagar
mylist = ["Maharashtra",["Mumbai","Pune","Nagpur"]]
print("These are some of the important cities in ", mylist[0]," - ", mylist[1][0], mylist[1][1], mylist[1][2])
These are some of the important cities in Maharashtra - Mumbai Pune Nagpur
mylist = ["potato", "banana", "orange", "banana", "pomegranate","banana"]
print( "The term banana has appeared ", count.mylist("banana"), " Times in this list")
The term banana has appeared 3 Times in this list
लिस्ट के भीतर जो चीजें हैं उनमे से केवल कुछ चीजें आपको प्रिंट करनी हो तो आप इंडेक्स नंबर दे कर उसे प्रिंट कर सकते हो इसे स्लाइस कहते हैं.
myList = [9, 7, 34, 67, 81, 40, 25]
print(myList[1:5])
[7, 34, 67, 81]
इस तरह पायथॉन के लिस्ट में आप बहुत सारी चीजें कर सकते हैं.
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें
टिप्पणी: केवल इस ब्लॉग का सदस्य टिप्पणी भेज सकता है.