Social Media

1/03/2019

Introduction to tuple data structure

Tuple is a sequential data structure.
As string and list tuple also support indexing and slicing.
The elements of tuple can be heterogeneous in nature.
Tuple is immutable data structure.
We have to use () to defining the tuple.
We cannot use append() method in tuple because it is immutable data structure.
example
t=(1,2,3,4)
print(type(t)) #tuple

Creating an empty tuple:
tuple() method can be used to create an empty tuple.
t=tuple()
print(type(t)) # tuple

Creating a tuple with single element:
t=(1,) # , is necessary if we are creating a tuple of single element, otherwise it will be int type.

Conversion of list into tuple:
We can also convert list data structure into tuple as shown below.
l=[1,2,3,4,5]
t=tuple(l)
print(t) # (1, 2, 3, 4, 5)


Be First to Post Comment !
Post a Comment