शुरुआती के लिए पायथन टुपल्स ट्यूटोरियल
टपल के मानों को वाक्य-विन्यास द्वारा 'अल्पविराम' द्वारा अलग किया जाता है। हालांकि यह आवश्यक नहीं है, कोष्ठक में मूल्यों के अनुक्रम को बंद करके टपल को परिभाषित करना अधिक सामान्य है। यह पायथन टुपल्स को अधिक आसानी से समझने में मदद करता है।
टुपल्स अपरिवर्तनीय हैं, और आमतौर पर, उनमें विषम तत्वों का एक क्रम होता है, जिन्हें अनपैकिंग या इंडेक्सिंग (या नामित टुपल्स के मामले में विशेषता द्वारा भी) के माध्यम से एक्सेस किया जाता है। सूचियाँ परिवर्तनशील होती हैं, और उनके तत्व आमतौर पर सजातीय होते हैं और सूची में पुनरावृति करके पहुँचा जा सकता है।
एक टपल बनाना
पायथन में, डेटा अनुक्रम के समूहीकरण के लिए कोष्ठक के उपयोग के साथ या बिना 'अल्पविराम' द्वारा अलग किए गए मानों के अनुक्रम को रखकर टुपल्स बनाए जाते हैं। टुपल्स में किसी भी संख्या में तत्व और किसी भी डेटाटाइप (जैसे तार, पूर्णांक, सूची, आदि) हो सकते हैं। टुपल्स को एक तत्व से भी बनाया जा सकता है, लेकिन यह थोड़ा मुश्किल है। कोष्ठक में एक तत्व का होना पर्याप्त नहीं है, इसे टपल बनाने के लिए एक अनुगामी 'अल्पविराम' होना चाहिए।
ध्यान दें - कोष्ठक के उपयोग के बिना पायथन टपल का निर्माण टपल पैकिंग के रूप में जाना जाता है।
# Python program to demonstrate # Addition of elements in a Set # Creating an empty tuple Tuple1 = () print('Initial empty Tuple: ') print (Tuple1) # Creating a Tuple with # the use of Strings Tuple1 = ('Geeks', 'For') print('
Tuple with the use of String: ') print(Tuple1) # Creating a Tuple with # the use of list list1 = [1, 2, 4, 5, 6] print('
Tuple using List: ') print(tuple(list1)) # Creating a Tuple # with the use of loop Tuple1 = ('Geeks') n = 5 print('
Tuple with a loop') for i in range(int(n)): Tuple1 = (Tuple1,) print(Tuple1) # Creating a Tuple with the # use of built-in function Tuple1 = tuple('Geeks') print('
Tuple with the use of function: ') print(Tuple1) # Creating a Tuple with # Mixed Datatypes Tuple1 = (5, 'Welcome', 7, 'Geeks') print('
Tuple with Mixed Datatypes: ') print(Tuple1) # Creating a Tuple # with nested tuples Tuple1 = (0, 1, 2, 3) Tuple2 = ('python', 'geek') Tuple3 = (Tuple1, Tuple2) print('
Tuple with nested tuples: ') print(Tuple3) # Creating a Tuple # with repetition Tuple1 = ('Geeks',) * 3 print('
Tuple with repetition: ') print(Tuple1)
आउटपुट:
Initial empty Tuple: () Tuple with the use of String: ('Geeks', 'For') Tuple using List: (1, 2, 4, 5, 6) Tuple with a loop ('Geeks',) (('Geeks',),) ((('Geeks',),),) (((('Geeks',),),),) ((((('Geeks',),),),),) Tuple with the use of function: ('G', 'e', 'e', 'k', 's') Tuple with Mixed Datatypes: (5, 'Welcome', 7, 'Geeks') Tuple with nested tuples: ((0, 1, 2, 3), ('python', 'geek')) Tuple with repetition: ('Geeks', 'Geeks', 'Geeks')
टुपल्स का संयोजन
टपल का संयोजन दो या दो से अधिक टुपल्स के जुड़ने की प्रक्रिया है। संयोजन '+' ऑपरेटर के उपयोग द्वारा किया जाता है। टुपल्स का संयोजन हमेशा मूल टपल के अंत से किया जाता है। अन्य अंकगणितीय ऑपरेशन टुपल्स पर लागू नहीं होते हैं।
ध्यान दें- केवल एक ही डेटाटाइप को संयोजन के साथ जोड़ा जा सकता है, एक सूची और एक टपल संयुक्त होने पर एक त्रुटि उत्पन्न होती है।
# Concatenaton of tuples Tuple1 = (0, 1, 2, 3) Tuple2 = ('Geeks', 'For', 'Geeks') Tuple3 = Tuple1 + Tuple2 # Printing first Tuple print('Tuple 1: ') print(Tuple1) # Printing Second Tuple print('
Tuple2: ') print(Tuple2) # Printing Final Tuple print('
Tuples after Concatenaton: ') print(Tuple3)
आउटपुट:
Tuple 1: (0, 1, 2, 3) Tuple2: ('Geeks', 'For', 'Geeks') Tuples after Concatenaton: (0, 1, 2, 3, 'Geeks', 'For', 'Geeks')
Tuple . का टुकड़ा करना
टुपल की स्लाइसिंग एक विशिष्ट श्रेणी या टुपल से उप-तत्वों का टुकड़ा लाने के लिए की जाती है। स्लाइसिंग सूचियों और सरणियों के लिए भी की जा सकती है। सूची में अनुक्रमित करने से एकल तत्व प्राप्त होता है जबकि स्लाइसिंग तत्वों का एक सेट प्राप्त करने की अनुमति देता है।
ध्यान दें- टुपल्स के अनुक्रम को उलटने के लिए नकारात्मक वृद्धि मूल्यों का भी उपयोग किया जा सकता है
# Slicing of a Tuple # Slicing of a Tuple # with Numbers Tuple1 = tuple('GEEKSFORGEEKS') # Removing First element print('Removal of First Element: ') print(Tuple1[1:]) # Reversing the Tuple print('
Tuple after sequence of Element is reversed: ') print(Tuple1[::-1]) # Printing elements of a Range print('
Printing elements between Range 4-9: ') print(Tuple1[4:9])
आउटपुट:
Removal of First Element: ('E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S') Tuple after sequence of Element is reversed: ('S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G') Printing elements between Range 4-9: ('S', 'F', 'O', 'R', 'G')
एक टुपल हटाना
Tuples अपरिवर्तनीय हैं और इसलिए वे इसके एक हिस्से को हटाने की अनुमति नहीं देते हैं। डेल () विधि का उपयोग करके संपूर्ण टपल को हटा दिया जाता है।
ध्यान दें- हटाने के बाद टपल की छपाई में त्रुटि होती है।
# Deleting a Tuple Tuple1 = (0, 1, 2, 3, 4) del Tuple1 print(Tuple1)
Traceback (most recent call last): File /home/efa50fd0709dec08434191f32275928a.py, line 7, in print(Tuple1) NameError: name ‘Tuple1’ is not defined
#पायथन