Python : Tuple in Hindi

  • Tuple element के sequence के set को define करता है।
  • Tuple immutable होता है। अर्थात यह unchangeable होता है।
  • tuple , List से fast होता है।

Creating a tuple

Tuple में store सभी items को comma (,) दवारा separate किया जाता है।
Tuple में store सभी items को parentheses ( ) के अंदर लिखा जाता है। parentheses ( ) का उपयोग करना optional है लेकिन इसका उपयोग करना एक अच्छा अभ्यास है।

tuple = ( )   # tuple literal
tuple1 = ("java", "python", "Html", "CSS")      # string Tuple
tuple2 = (1, 5, 7, 9, 3)       # int tuple 
tuple3= (2.5, 8.6, 6.79, 3.65)   # float tuple
tuple4 = (True, False, False)   # bool tuple
tuple5= (25,6,9,(".net",59,"JS"))     # nested Tuple

print (tuple)
print(tuple1)
print(tuple2)
print(tuple3)
print(tuple4)
print(tuple5)

Output : 
( )
('java', 'python', 'Html', 'CSS') 
(1, 5, 7, 9, 3) 
(2.5, 8.6, 6.79, 3.65) 
(True, False, False)
(25, 6, 9, ('.net', 59, 'JS'))

Tuple functions

SNFunctionDescription
1len(tuple)यह tuple की length को calculates करता है।
2max(tuple)यह tuple की maximum element को return करता है।
3min(tuple)यह tuple की minimum element को return करता है।
4sorted(tuple)दिए गए sequence को sort करके return करना।
y=(95,25,78,36,28,95,22,20)

print(len(y))
print(max(y))
print(min(y))
print(sorted(y))
Output : 
8
95
20
[20, 22, 25, 28, 36, 78, 95, 95]

Tuple Indexing & slicing

tuple में Indexing और slicing , list की तरह similar होते है। tuple में indexing o से शुरू होता है और length(tuple)-1 तक जाता है।
[ ] operator का उपयोग करके टपल में आइटम एक्सेस करते है। tuple मे एक से अधिक item को एक्सेस करने के लिए कोलन : ऑपरेटर का उपयोग करते है।

t=(25, 89, "hy", "java", 2.8, 78)
print (t[0])
print (t[1])
print (t[2])
print (t[3])
print (t[4])
print (t[5])

OUTPUT :
25
89
hy
java
2.8
78
t=(25, 89, "hy", "java", 2.8, 78)
#print item1 to end
print(t[1:])
#print item 0 to 3
print(t[:4])
#print item 1 to 4 element
print(t[1:5])
#print item 0 to 6 and take step of 2
print(t[0:6:2])
OUTPUT :
(89, 'hy', 'java', 2.8, 78)
(25, 89, 'hy', 'java')
(89, 'hy', 'java', 2.8)
(25, 'hy', 2.8)

Tuple Concatenation & Repetition

दो या दो से अधिक tuple को आपस में join करता है। इसके लिए + operator का प्रयोग किया जाता है।

#Concatenation
x=(1,2,5,6)
y=(95,25,78,36,28,95,22,20)
a=(89,566,26,35)
z=x+y+a
print(z)
OUTPUT :
(1, 2, 5, 6, 95, 25, 78, 36, 28, 95, 22, 20, 89, 566, 26, 35)

Repetition ऑपरेटर का प्रयोग tuple element को multiple time repeat करने के लिए किया जाता है।

#Repetition
x=(1,2,5,6)
print (x*3)
OUTPUT :
(1, 2, 5, 6, 1, 2, 5, 6, 1, 2, 5, 6)

Tuple Methods

MethodDescription
count()tuple में एक या एक से अधिक बार स्टोर value के आने के नंबर को return करता है।
index()tuple मे specific value को search करता है और उसकी position को return करता है।
#दिए गए tuple में 2 , 3 time स्टोर है।
z=(2,5,8,95,100,2500,2,58,2)
y=z.count(2)
print(y)
OUTPUT : 
3
#दिए गए tuple में 25 की index वैल्यू को return करेगा। 
y=(95,25,78,36,28,95,22,20)
y.index(25)
OUTPUT : 
1