Python की मूल बातें

Python में टपल

Tuple एक ordered collection है, जैसे list, लेकिन इसे बनने के बाद बदला नहीं जा सकता, इसलिए यह coordinates जैसे fixed groups के लिए अच्छा है।

में टपल क्या है?

Tuple एक ordered collection है, जैसे list, लेकिन इसे बनने के बाद बदला नहीं जा सकता, इसलिए यह coordinates जैसे fixed groups के लिए अच्छा है।

एक tuple में values group करें, उन्हें unpack करें और function से कई values return करें।

इसका उपयोग कब करें?

  • चलने योग्य code से Python syntax सीखें।
  • बड़ी programming समस्याओं से पहले मजबूत आधार बनाएँ।
  • स्थानीय setup के बिना किसी विचार को तुरंत जाँचें।

देखें यह कैसे काम करता है

टपल बनने के बाद बदला नहीं जा सकता। उसे बदलने की कोशिश करें, फिर उसके मानों को अलग-अलग वेरिएबल में अनपैक करें।

उदाहरण कोड

कोड चलाएँ →
main.py
# A tuple is an ordered collection that cannot be changed after it is created.
point = (3, 4)
colors = ("red", "green", "blue")

print(point[0], point[1])
print(len(colors), colors[-1])

# Unpacking assigns each item to its own variable.
x, y = point
print(f"x = {x}, y = {y}")

# Functions can return several values as a tuple.
def min_max(values):
    return min(values), max(values)

low, high = min_max([7, 2, 9, 4])
print(low, high)

# Swap two variables without a temporary one.
a, b = 1, 2
a, b = b, a
print(a, b)

# Trying to change a tuple raises an error.
try:
    point[0] = 10
except TypeError as error:
    print("Error:", error)

अपेक्षित आउटपुट

3 4
3 blue
x = 3, y = 4
2 9
2 1
Error: 'tuple' object does not support item assignment

यह कैसे काम करता है

Unpacking हर item को अपनी अलग variable में रखता है, और यही तरीका function के कई values return करने और a, b = b, a से दो variables swap करने में भी काम आता है।

मान बदलें और Python इंस्टॉल किए बिना CodeUtility ऑनलाइन Python कंपाइलर में प्रोग्राम चलाएँ।

अभ्यास के कार्य

बड़े dataset पर जाने से पहले input बदलें और edge cases की जाँच करें।

  1. Code चलाने से पहले output का अनुमान लगाएँ।
  2. खाली और गलत input संभालें।
  3. Logic को function में रखें और tests जोड़ें।
Python IDE में चलाएँ →

चैलेंज से अभ्यास करें

इस पाठ में सीखी बात से एक असली समस्या हल करें। हर चैलेंज ब्राउज़र में चलता है, तुरंत टेस्ट और AI रिव्यू के साथ।

सभी चैलेंज