Tuple In Python



Tuple is a collection data type in Python that is required when you are required to store multiple values. A tuple is like a list that is used to collect multiple values but its values cannot be changed. For example: If you want to store the Roll No of students which remains the same here till graduation, here tuple is used to store roll no. values. Tuple values are defined in parenthesis which is used to differentiate tuple from the list. You can access values through indexes however you cannot assign value to the tuple. If you want to change the value of a tuple then you have to redefine it. Tuple supports indexing, slicing, and concatenation operations.

                                                                                  


roll_no = (1,2,3,4,6)
print(roll_no)
print(type(roll_no))

# get value with indexing
roll_no[4]

# slicing
print(roll_no[0:3])
print(roll_no[-1])

# Iterate tuple values
for no in roll_no:
    print(no)
    
# tuple is not changeablel
roll_no[2] = 10

# concatenation one tuple with other tuple
new_roll = (7,)
roll_no = roll_no+new_roll
print(roll_no)
print(type(roll_no))

Github:
https://github.com/asadraza825/python/blob/master/tuple.ipynb
Tags: Python, Python Programming,  Python Tuple, Tuple, Tuple Operations

Next Post Previous Post
No Comment
Add Comment
comment url