Python Tuples

A tuple is a standard data type in python. it is just like a list wchich is a collection of elements. it is written by using parentheses ( ).
it is ordered collection of data items (collection of items in a sequence orderr) just like list.
in python list is changeable but tuple is unchangeable ( means it can not be change once initialize elements in tuples).

Tuple is a collection of elements (items)

in python tuple gives us the benefit of create a collection (group) of items under a one common name.
a tuple can be of any type.

a string data type tuple
for example: a tuple can be of shopping products
shopptuple = ("iphone 8", "samsung led tv", "rolex watch", "hp laptop")

a int data type tuple
for example: a tuple can be of student marks
studentmarkstuple = (76, 81, 93, 64, 87)

a tuple can be of mix of various data types such as string, int etc.
for example: a tuple of a mixed of data types
mixtuple = ("steve", False, 71, 31.2)


Access Tuples items

in python we access the tuples items by its index position number just we do in lists. indexing of items are of two types in python are positive indexing and negative indexing.
Positive indexing of tuple items
in python positive indexing starts with (0, 1, 2, 3) is used for access tuple items.
for example:
studentstuple = ("jim", "dany", "sara", "rozy", "tom")



you think of that why we use [0] index number instead of [1] for 'jim'.
because in python the tuples starts with 0 index for first item . 1 index for second item so on...

Negative indexing of tuple items
in python negative indexing ( -1, -2, -3, -4) is used for access tuple items in reverse order.
it means the last item in the tuple is at the [-1] index. second last item at the [-2] index.
third last item at the [-3] index and so on..
for example:
studentstuple = ("jim", "dany", "sara", "rozy", "tom")




Insert, Update, Delete Tuples items

in lists we can perform insert, update, deltete operations on items but in tuples we can not perform these operations.
because tuple is immutable (once create then we can not change these).
for this reason tuple is useful in the program (app) where we need a collection of items for read only purpose.

if we do update operation on the tuple it will give the error: for example:
studentstuple = ("jim", "dany", "sara", "rozy", "tom")

studentstuple[0] = "john"


Traceback (most recent call last):
  File "./program.py", line 3, in <module>
TypeError: 'tuple' object does not support item assignment        		     					    			
we can use insert(), remove() functions only on lists not on tuples.

we can remove a tuple from the program by using del keyword. for example:
studentstuple = ("jim", "dany", "sara", "rozy", "tom")

print("before deleting the tuple is :")


del studentstuple

print("after deleting the tuple is : ")



before deleting the tuple is :
('jim', 'dany', 'sara', 'rozy', 'tom')
after deleting the tuple is :         		     					    			
after deletion, the tuple become undefined.