Python Tuple Exercises
Practice these Python tuple exercises to improve your understanding. Click "Show Result" to check the solution for each exercise.
इन Python tuple अभ्यासों को करें और अपनी समझ बेहतर बनाएं। प्रत्येक अभ्यास के लिए "Show Result" पर क्लिक करके समाधान देखें।
Exercise 1: 1. Create a tuple named colors
containing three color names and print it.
अभ्यास 1: 1. तीन रंगों के नामों वाला एक tuple colors
बनाएं और उसे प्रिंट करें।
colors = ('red', 'green', 'blue') print(colors) # Output: ('red', 'green', 'blue')
Exercise 2: 2. Access the second item in the tuple colors
.
अभ्यास 2: 2. tuple colors
में दूसरे item को access करें।
colors = ('red', 'green', 'blue') print(colors[1]) # Output: green
Exercise 3: 3. Try to change the first item of the tuple colors
to 'yellow'. What happens?
अभ्यास 3: 3. tuple colors
के पहले item को 'yellow' में बदलने की कोशिश करें। क्या होगा?
colors = ('red', 'green', 'blue') try: colors[0] = 'yellow' except TypeError as e: print(e) # Output: 'tuple' object does not support item assignment
Exercise 4: 4. Concatenate two tuples t1 = (1, 2)
and t2 = (3, 4)
and print the result.
अभ्यास 4: 4. दो tuples t1 = (1, 2)
और t2 = (3, 4)
को जोड़कर प्रिंट करें।
t1 = (1, 2) t2 = (3, 4) t3 = t1 + t2 print(t3) # Output: (1, 2, 3, 4)
Exercise 5: 5. Find the length of tuple t3
.
अभ्यास 5: 5. tuple t3
की लंबाई पता करें।
t3 = (1, 2, 3, 4) print(len(t3)) # Output: 4
Exercise 6: 6. Check if the value 2 is present in tuple t3
.
अभ्यास 6: 6. देखें कि क्या value 2 tuple t3
में मौजूद है।
t3 = (1, 2, 3, 4) print(2 in t3) # Output: True
Exercise 7: 7. Loop through all elements in tuple t3
and print them one by one.
अभ्यास 7: 7. tuple t3
के सभी elements पर loop चलाकर उन्हें एक-एक करके प्रिंट करें।
t3 = (1, 2, 3, 4) for item in t3: print(item) # Output: 1 2 3 4
Exercise 8: 8. Find the index of the value 3 in tuple t3
.
अभ्यास 8: 8. tuple t3
में value 3 का index पता करें।
t3 = (1, 2, 3, 4) print(t3.index(3)) # Output: 2
Exercise 9: 9. Count how many times the value 2 appears in tuple t3
.
अभ्यास 9: 9. tuple t3
में value 2 कितनी बार आता है, इसे गिनें।
t3 = (1, 2, 3, 4, 2) print(t3.count(2)) # Output: 2
Exercise 10: 10. Convert the list [5, 6, 7]
into a tuple and print it.
अभ्यास 10: 10. list [5, 6, 7]
को tuple में बदलें और प्रिंट करें।
lst = [5, 6, 7] t = tuple(lst) print(t) # Output: (5, 6, 7)