
Table of Contents All Chapters 1. Python Set 2. Iterate Set items 3. Set Methods 3.1. Add items...
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. Python Set 2. Iterate Set items 3. Set Methods 3.1. Add items...
Table of Contents All Chapters 1. Python Tuple 2. Access Tuple Items 3. Update Tuple 4. Iterate Tuple Items 5....
Table of Contents All Chapters 1. Python List 2. Nested List 3. Range List 4. List Indexing 5. list Methods...
Table of Contents All Chapters 1. Python String 2. String Indexing 3. String Slicing 4. String Concatenation 5. String Formatting...
Table of Contents All Chapters 1. Python Loops 2. While Loop 2.1. Nested While Loop 3. For Loop...
Table of Contents All Chapters 1. If-else Statements 1.1. If Statement 1.2. Else Statement ...
Table of Contents All Chapters 1. Python Operators 2. Types of Operators in Python 2.1. Arithmetic Operators ...
Table of Contents All Chapters 1. Data Type 2. Types of Data Type 2.1. Numeric Data Type ...
Table of Contents All Chapters 1. Scikit Learn 2. Install Scikit-learn 3. Features of Scikit-learn Scikit-learn Tutorial Scikit-learn Hamburger Toggle...