Skip to main content

tuple in python पायथन में टपल, tuple operations टपल ऑपरेशन , tuple functions and methods टपल फ़ंक्शन और विधियाँ

In Python, a tuple is a collection of ordered, immutable elements. Tuples are similar to lists, but unlike lists, once a tuple is created, its elements cannot be changed. Tuples are defined by placing a sequence of elements separated by commas inside parentheses ().
पायथन में, टपल क्रमबद्ध, अपरिवर्तनीय तत्वों का एक संग्रह है। टपल्स सूचियों के समान हैं, लेकिन सूचियों के विपरीत, एक बार टपल बन जाने के बाद, इसके तत्वों को बदला नहीं जा सकता है। टपल्स को कोष्ठक () के अंदर अल्पविराम द्वारा अलग किए गए तत्वों के अनुक्रम को रखकर परिभाषित किया गया है।

# Creating a tuple टपल तैयार करना :-

my_tuple = (1, 2, 3, "apple", [4, 5])

# Single-element tuple (note the trailing comma) एकल-तत्व टपल (अंतिम अल्पविराम पर ध्यान दें)

single_element_tuple = (10,)

# Tuples can be nested ट्पल्स को नेस्ट किया जा सकता है

nested_tuple = (1, (2, 3), [4, 5])

tuple operations टपल ऑपरेशन:-

1. Concatenation जोड़ना:- We can concatenate two or more tuples using the + operator.
हम + ऑपरेटर का उपयोग करके दो या दो से अधिक टपल्स को जोड़ सकते हैं।

t1 = (1, 2, 3)
t2 = (4, 5, 6)
t3 = t1 + t2
print(t3) # Output: (1, 2, 3, 4, 5, 6)

2. Repetition दोहराव:- We can repeat a tuple a given number of times using the * operator.
हम * ऑपरेटर का उपयोग करके किसी टपल को दी गई संख्या तक दोहरा सकते हैं।

t = (1, 2, 3)
print(t * 3) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)

3. Indexing अनुक्रमाणिका:- We can access elements of a tuple using their index (starting from 0).
हम टपल के तत्वों को उनके इंडेक्स (0 से शुरू) का उपयोग करके एक्सेस कर सकते हैं।

t = (10, 20, 30, 40)
print(t[0]) # Output: 10
print(t[2]) # Output: 30

4. Slicing स्लाइसिंग:- Tuples support slicing to access a range of elements.
टपल्स विभिन्न प्रकार के तत्वों तक पहुँचने के लिए स्लाइसिंग का समर्थन करते हैं।

t = (10, 20, 30, 40, 50)
print(t[1:4]) # Output: (20, 30, 40)

5. tuple length टपल की लंबाई:- Finding the number of elements present in a tuple using `len()`.
`len()` का उपयोग करके टपल में उपस्थित तत्वों की संख्या ज्ञात की जा सकती हैं।

t = (1, 2, 3, 4)
print(len(t)) # Output: 4

6. Membership Testing मेम्बरशिप की जांच:- We can check whether a value is present in a tuple using the in keyword.
हम in कीवर्ड का उपयोग करके जांच सकते हैं कि टपल में कोई मान उपस्थित है या नहीं।

t = (10, 20, 30)
print(20 in t) # Output: True
print(50 in t) # Output: False

7. Iteration दोहराव:- We can iterate over the elements of a tuple using a for loop.
हम फॉर लूप का उपयोग करके टपल के तत्वों पर पुनरावृति कर सकते हैं।

t = (1, 2, 3)
for element in t:
  print(element)
# Output:
1
2
3

8. tuple unpacking टपल अनपैकिंग:- We can assign the elements of a tuple to individual variables.
हम टपल के तत्वों को अलग-अलग वेरिएबल्स में निर्दिष्ट कर सकते हैं।

my_tuple = (1, 2, 3, "apple", [4, 5])
a, b, c, d, e = my_tuple
print(a) # Output: 1
print(b) # Output: 2
print(c) # Output: 3
print(d) # Output: “apple”
print(e) # Output: [4, 5]

9. Conversion between List and Tuple टपल और सूचि के बीच कन्वर्शन:- We can perform conversion between tuples and lists using list() and tuple().
हम list() और tuple() का उपयोग करके टुपल्स और सूचियों के बीच कन्वर्शन कर सकते हैं।

# Convert a tuple to a list
t = (1, 2, 3)
l = list(t)
print(l) # Output: [1, 2, 3]

# Convert a list to a tuple
l = [4, 5, 6]
t = tuple(l)
print(t) # Output: (4, 5, 6)

tuple functions and methods टपल फ़ंक्शन और विधियाँ:-

1. tuple():- Converts a sequence (like a list, string, or set) into a tuple.
यह फंक्शन किसी अनुक्रम (जैसे सूची, स्ट्रिंग या सेट) को टपल में परिवर्तित करता है।

l = [1, 2, 3]
t = tuple(l)
print(t) # Output: (1, 2, 3)

2. index():- Returns the index of the first occurrence of a specified value. Raises a ValueError if the value is not found.
निर्दिष्ट मान की पहली घटना का सूचकांक लौटाता है। यदि मान नहीं मिलता है तो ValueError प्रदर्शित करता है।

t = (1, 2, 3, 4, 2)
print(t.index(2)) # Output: 1


3. min():- Returns the smallest element present in the tuple (if elements are comparable).
यह फंक्शन टपल में उपस्थित सबसे छोटा तत्व लौटाता है (यदि तत्व तुलनीय हैं)।

t = (1, 5, 3, 7)
print(min(t)) # Output: 1

4. max() :- Returns the largest element present in the tuple (if elements are comparable).
यह फंक्शन टपल में उपस्थित सबसे बड़ा तत्व लौटाता है (यदि तत्व तुलनीय हैं)।

t = (1, 5, 3, 7)
print(max(t)) # Output: 7

5. sum():- Returns the sum of all numeric elements in the tuple.
यह फंक्शन टपल में सभी संख्यात्मक तत्वों का योग लौटाता है।

t = (1, 2, 3, 4)
print(sum(t)) # Output: 10


6. count():- Returns the number of times a specified value appears in the tuple.
निर्दिष्ट मान के टपल में प्रकट होने की संख्या लौटाता है।

t = (1, 2, 3, 2, 2, 4)
print(t.count(2)) # Output: 3

7. sorted():- Although tuples are immutable, you can sort them using the sorted() function, which returns a list of sorted elements.
यद्यपि टपल अपरिवर्तनीय होते हैं, फिर भी आप उन्हें sorted() फ़ंक्शन का उपयोग करके सॉर्ट कर सकते हैं, जो सॉर्ट किए गए तत्वों की सूची लौटाता है।

t = (4, 2, 5, 1)
sorted_t = sorted(t)
print(sorted_t) # Output: [1, 2, 4, 5]

8. any():- Returns True if at least one element of the tuple is True. For non-boolean values, Python considers non-zero numbers, non-empty strings, etc., as True.
यदि टपल का कम से कम एक तत्व सत्य है, तो सत्य लौटाता है। गैर-बूलियन मानों के लिए, पायथन गैर-शून्य संख्याओं, गैर-खाली स्ट्रिंग्स आदि को सत्य मानता है।

t = (0, 0, 3)
print(any(t)) # Output: True

9. all():- Returns True if all elements in the tuple are True (non-zero, non-empty).
यदि टपल में सभी तत्व सत्य (गैर-शून्य, गैर-रिक्त) हैं तो सत्य लौटाता है।

t = (1, 2, 3)
print(all(t)) # Output: True

t = (1, 0, 3)
print(all(t)) # Output: False

10. reversed():- Returns an iterator that accesses the elements of the tuple in reverse order.
एक इटरेटर लौटाता है जो टपल के तत्वों को उल्टे क्रम में एक्सेस करता है।

t = (1, 2, 3, 4)
rev_t = tuple(reversed(t))
print(rev_t) # Output: (4, 3, 2, 1)

Tuple as a swap function (Tuple Unpacking):-

We can swap two variables using a tuple:- 
हम टपल का उपयोग करके दो वेरिएबल्स को स्वैप कर सकते हैं:-

x, y = 5, 10
x, y = y, x

print("x:", x)  # Output: x: 10
print("y:", y)  # Output: y: 5

This method utilizes tuple unpacking to swap the values directly. 

यह विधि मानों को सीधे स्वैप करने के लिए टपल अनपैकिंग का उपयोग करती है। 

Comments

Popular posts from this blog

Filter function of python

filter function:-  Imagine we have a bunch of things, like a list of numbers, a collection of fruits, or any other group of items. Now, let's say we want to pick out only certain items from that group based on a specific condition. This is where the filter function comes in handy. Let's say we have a list of numbers, and we want to filter out only the even numbers. We define a condition-checking function that checks if a number is even. Then, we pass this function and the list of numbers to the filter function. It goes through each number, applies the condition-checking function, and keeps only the numbers that are even. Finally, it gives us a new list containing only the even numbers. def is_even(num):     return num % 2 == 0 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filtered_numbers = list(filter(is_even, numbers)) print(filtered_numbers) Output [2, 4, 6, 8, 10] We can also utilize lambda expressions to define filtering conditions directly within the filter function. nu...

The Python Interpreter पायथन इंटरप्रेटर

The Python interpreter is a program that executes Python code. It reads and interprets Python scripts or interactive commands, converting them into machine-readable bytecode for the computer to execute.The interpreter is a crucial component for running Python code, facilitating both learning and development processes. Users can interact with the interpreter in two primary modes: पायथन इंटरप्रेटर एक प्रोग्राम है जो पायथन कोड को निष्पादित करता है। यह पायथन स्क्रिप्ट या इंटरैक्टिव कमांड को पढ़ता है और व्याख्या करता है, उन्हें कंप्यूटर द्वारा निष्पादित करने के लिए मशीन-पठनीय बाइटकोड में परिवर्तित करता है। दुभाषिया पायथन कोड चलाने के लिए एक महत्वपूर्ण घटक है, जो सीखने और विकास दोनों प्रक्रियाओं को सुविधाजनक बनाता है। उपयोगकर्ता इंटरप्रेटर के साथ दो प्राथमिक मोड में बातचीत कर सकते हैं:- 1. Interactive Mode:- In this mode we can launch the Python interpreter in our terminal or command prompt without specifying a script.This mode allows us to enter Python commands and see immediate results...

Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह

Flow of execution represents a general outline of Python program execution. The specific flow can vary based on the program's structure, control flow statements (if, else, for, while), and function calls. निष्पादन का प्रवाह पायथन प्रोग्राम निष्पादन की एक सामान्य रूपरेखा का प्रतिनिधित्व करता है। विशिष्ट प्रवाह प्रोग्राम की संरचना, नियंत्रण प्रवाह विवरण (यदि, अन्यथा, के लिए, जबकि), और फ़ंक्शन कॉल के आधार पर भिन्न हो सकता है। 1.) Start: The program begins execution. 1.) प्रारंभ: प्रोग्राम का निष्पादन प्रारंभ होता है। 2.) Import Modules: If necessary, the program imports external modules or libraries. 2.) मॉड्यूल आयात करें: यदि आवश्यक हो, तो प्रोग्राम बाहरी मॉड्यूल या लाइब्रेरी आयात करता है। 3.) Define Functions: Any functions used in the program are defined. 3.) फंक्शन्स को परिभाषित करें: प्रोग्राम में उपयोग किए गए किसी भी फंक्शन को परिभाषित किया जाता है। 4.) Main Program: The main body of the program starts. 4.) मुख्य प्रोग्राम: प्रोग्राम का मुख्य भाग प्रारंभ होता है। 5.) Initializat...