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

Programming with Python Language Tutorial available in hindi and english पाइथन प्रोग्रामिंग टुटोरिअल हिंदी एवं अंग्रेजी भाषा में

 Unit -01 1. Introduction to python (पाइथन भाषा का परिचय) 2. History of Python Programming Language पाइथन प्रोग्रामिंग लैंग्वेज का इतिहास 3. Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं 4. Python’s Programming Language's Popularity पाइथन प्रोग्रामिंग लैंग्वेज की लोकप्रियता 5.The Future of Python Programming Language पायथन प्रोग्रामिंग लैंग्वेज का भविष्य 6. The Python Interpreter पायथन इंटरप्रेटर 7. The Python IDLE पाइथन आईडीएलई 8. Installation process of Python IDLE पायथन आईडीएलई की इंस्टालेशन प्रक्रिया 9. Dynamically typed and strongly typed features of python गतिशील रूप से टाइप और दृढ़ता से टाइप की गई भाषा पायथन 10. Basic Data Types of Python पायथन के बेसिक डेटा टाइप 11. Variables in python पायथन में वेरिएबल 12. Expressions in Python पायथन में अभिव्यक्तियां 13. Statements in Python पायथन में कथन 14. Operator's in Python पायथन में ऑपरेटर्स 15. Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह 16...

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...

Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं

Python is a versatile and popular programming language known for its simplicity and readability. It has a wide range of features that make it suitable for various types of projects. Here are some key features of the Python programming language:- पायथन एक बहुमुखी और लोकप्रिय प्रोग्रामिंग भाषा है जो अपनी सरलता और पठनीयता के लिए जानी जाती है। इसमें कई प्रकार की विशेषताएं हैं जो इसे विभिन्न प्रकार की परियोजनाओं के लिए उपयुक्त बनाती हैं। पायथन प्रोग्रामिंग भाषा की कुछ प्रमुख विशेषताएं निम्न हैं:- 1. Open Source ओपन सोर्स:-   Python is an open-source language, which encourages collaboration and allows developers to contribute to its ongoing development. पायथन एक ओपन-सोर्स भाषा है, जो सहयोग को प्रोत्साहित करती है और डेवलपर्स को इसके चल रहे विकास में योगदान करने की अनुमति देती है। 2. Integrated Development & Learning Environments एकीकृत विकास एवं अध्ययन वातावरण  (IDLEs/IDEs):-  Python has several powerful IDLEs/IDEs, such as PyCharm, VSCode, and Jupyter, that provide too...