Professional Documents
Culture Documents
Tuple
Tuple
Tuple
Tuples
The diffence diffence between the two is that we can't change the elements of tuple we can't
change the elements of tuple once it is assigned whereas in the list, elements can be changed
Tuple is immutable
In [2]:
t1 = (1,2,"hello",True)
In [3]:
t1
In [3]:
# Access Tuple Items with Index Numbering
t1[1]
Out[3]: 2
In [4]:
# Negative Indexing refers the index from last item
t1[-1]
Out[4]: True
In [6]:
# Range of Indexes
t1[1:4]
In [8]:
# Range of Negative Index
t1[-4:-1]
In [4]:
# length of Tuple
len(t1)
Out[4]: 4
In [6]:
# To Add items
t1 = (1,2,"hello",True)
t1
In [7]:
t1[3] = False
---------------------------------------------------------------------------
<ipython-input-7-c7dfc71f5c9d> in <module>
In [8]:
# To remove the Tuple
del t1
In [9]:
# Joining twot Tuples
t1 = (1,2,"hello",True)
t2 =(False,"world")
In [6]:
t3 = t1+t2
In [7]:
t3
In [8]:
# Count - Number of times a word is getting repeated
t3.count(2)
Out[8]: 1
In [23]:
# to find the index number of Item in the tuple
t3.index("hello")
Out[23]: 2
In [11]:
# tuple with one value end with Comma
t4 = (2,)
In [12]:
t4
Out[12]: (2,)
# create a tuple with one value and one tuple and one list
In [14]:
t5
In [17]:
# Repeat the elements in a tuple for a given number of times using the * operator.
t6 = (("hello",)* 4)
In [18]:
t6
In [19]:
# Tuple Sort
t7 = (3,2,1,4,6,5)
In [20]:
t8 = sorted(t7)
In [21]:
t8
Out[21]: [1, 2, 3, 4, 5, 6]
In [22]:
# Largest Elements
max(t8)
Out[22]: 6
In [23]:
# Smallest Elements
min(t8)
Out[23]: 1
In [24]:
# using tuple we can create for loops
t =(1,2,3,4,5)
print('tuple')
for x in t:
print(x)
tuple
In [ ]: