
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
6. Nested Tuple
Python में Tuple, data का एक ordered और immutable collection होता है जो Elements को round bracket के अंदर comma separate करके Store करता है, Tuple का use हम तब करते हैं जब हमें data को unchangeable form मे store करना होता है।
num_tuple = (4, 3, 8)
print(num_tuple)
# Output : (4, 3, 8)
indexing का use करके हम tuple के elements को access कर सकते हैं, indexing 0 से शुरू होती है tuple में negative indexing भी possival है यह -1 से शुरू होती है जो की last value को represent करता है
fruits = ("banana", "apple", "guava")
print(fruits[0])
print(fruits[1])
print(fruits[-1])
# Output : banana
apple
guava
Tuple immutable होने के कारण हम इसे direct update तो नहीं कर सकते but अगर tuple के अंदर mutable elements हो जैसे list तो उन elements को modified किया जा सकता है
num = (4, 6, [5, 2])
num[2][0] = 1
print(num)
# Output : (4, 6, [1, 2])
tuple के items को iterate करने के लिए हम loop का use करते हैं
fruits = ("banana", "apple", "guava")
for i in fruits:
print(i)
# Output : banana
# apple
# guava
+ operator का use करके हम दो या दो से ज्यादा tuples को join कर सकते है
num1 = (4, 3, 8)
num2 = (2, 1, 5)
print(num1 + num2)
# Output : (4, 3, 8, 2, 1, 5)
जब हम tuple के अंदर भी tuple का use करते हैं तो इसे nested tuple कहा जाता है
num1 = (4, 3, (2, 1, 5), 8)
print(num1)
# Output : (4, 3, (2, 1, 5), 8)
python मे tuple के लिए कुछ ही build-in methods होते है, क्योंकि tuple immutable होता है।
Syntex:
method को किसी भी variable के साथ use करने के लिए dot ( . ) का use करते है।
Example:
count() : इस method का use करके हम tuple के किसी element को काउंट कर सकते हैं। कि वो element, tuple में कितनी बार आया है.
my_tuple = (1, 2, 3, 2, 4, 2, 5)
count_of_2 = my_tuple.count(2)
print(f"Count of 2: {count_of_2}") # Output: Count of 2: 3
count_of_6 = my_tuple.count(6)
print(f"Count of 6: {count_of_6}") # Output: Count of 6: 0
another_tuple = ('apple', 'banana', 'apple', 'orange')
count_apple = another_tuple.count('apple')
print(f"Count of 'apple': {count_apple}") # Output: Count of 'apple': 2
यहाँ कुछ tuple के methods दिए गए है जिन्हे आप इसी तरह से tuple के साथ use करके इनके result को देख सकते है।
my_tuple1 = (1, 2, 3, 4, 5)
total_sum = sum(my_tuple)
print(f"Sum of elements: {total_sum}")
# Output: Sum of elements: 15
my_list = [1, 2, 3]
new_tuple_from_list = tuple(my_list)
print(f"Tuple from list: {new_tuple_from_list}")
# Output: Tuple from list: (1, 2, 3)
my_tuple2 = (10, 5, 20, 3)
minimum_value = min(my_tuple)
print(f"Minimum value: {minimum_value}")
# Output: Minimum value: 3
my_tuple3 = (5, 2, 8, 1, 9)
sorted_list = sorted(my_tuple)
print(f"Original tuple: {my_tuple}")
# Output: Original tuple: (5, 2, 8, 1, 9)
print(f"Sorted list: {sorted_list}")
# Output: Sorted list: [1, 2, 5, 8, 9]
my_tuple4 = (1, 2, 3, 'hello')
length = len(my_tuple)
print(f"Length of the tuple: {length}")
# Output: Length of the tuple: 4
उम्मीद करते है कि आपको python tuple अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...
Table of Contents All Chapters 1. Modules in Python 2. Pre-defined Modules 2.1. math 2.2. random...
Table of Contents All Chapters 1. Iterators 1.1. Iterable 1.2. Iterator 1.3. Iterator Protocol...
Table of Contents All Chapters 1. Polymorphism 2. Key Concepts of Polymorphism 3. Types of Polymorphism 3.1. Polymorphism...
Table of Contents All Chapters 1. Inheritance 2. Concepts of Inheritance 3. Types of Inheritance 3.1. Single Inheritance...
Table of Contents All Chapters 1. Classes and Objects 2. Class in Python 3. Class Structure 3.1. Attributes...
Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...
Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...