Tuple in Python

Tuple is a collection in python that is ordered but its values can not be changed.
tuple can be declared as following.

myTuple=("Red","Blue","Black")
print(myTuple)

    Output:
    (‘Red’, ‘Blue’, ‘Black’)

similarly as list items can be accessed on the given index.

myTuple=("Red","Blue","Black")
print(myTuple[1])

    Output:
    Blue

Elements of a touple can not be appended , inserted ,removed or deleted as tuple is not changable.

so myTuple[0]=”Black” gives the error.

The elements of the tuple can also be printed by using for loop as in List.

myTuple=("Red","Blue","Black")
for i in myTuple:
print (i)

    Output:
    Red
    Blue
    Black

we can find the index of the given element and may find the length of the tuple as in List.

myTuple=("Red","Blue","Black")
print(myTuple.index("Blue")) 
print(len(myTuple))

    Output:
    1
    3

we can not change the elements of the tuple so del not works on elements of the tuple but whole tuple may be deleted

myTuple=("Red","Blue","Black")
del(myTuple)
print(myTuple)

    Output: error [ as tuple delete and it is no more to print]

In List and Tuple an item exist or not can be checked without loop.

myTuple=("Red","Blue","Black")
if "Blue" in myTuple:
print("exist")
else:
print("Not exists")

    Output:
   exists (as items exists in the tuple same can be implemented in List also.) 

Click Here

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial