Tuple is defined as an immutable list of data items. Once created its items can't be altered. So basically we use it in some specific situation.
Tuple is defined as an immutable list of data items. Once created tuple cannot be modified.
"""
created_at : 16 Feb 2017.
updated_at : 16 Feb 2017.
aim of script : To understand the concept of tuples in Python.
coded by : Rishikesh Agrawani.
Python version : 2.7.12
"""
#Interactive mode programming - #1
>>> t = (1,2,3,4,5) #1st way of creation
>>> print t
(1, 2, 3, 4, 5)
>>>
>>> type(t)
<type 'tuple'>
>>>
>>> t[0]
1
>>> t[3]
4
>>> t[3] = 5 #Modification of objects/items of a tuple object is not allowed
Traceback (most recent call last):
File "", line 1, in
TypeError: 'tuple' object does not support item assignment
>>>
>>> t2 = 7,5,3,2,1 #2nd way of creation
>>> print t2
(7, 5, 3, 2, 1)
>>>
>>> t3 = 3,2,1,7,9, #3rd way of creation
>>> print t3
(3, 2, 1, 7, 9)
>>>
>>> t+t3 #Adding 2 tuples
(1, 2, 3, 4, 5, 3, 2, 1, 7, 9)
>>>
>>> len(t) #Counting number of items in a tuple
5
>>>
#Interactive mode programming - #2
>>> l = xrange(1,11)
>>> print l
xrange(1, 11)
>>>
>>> list(l)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>
>>> lst = list(l)
>>> t = tuple(lst) #4th way of creating a tuple from a list
>>> t2 = tuple(l) #5th way of creating a tuple from an xrange object
>>>
>>> print t
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>>
>>> print t2
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>>
>>> for i in t:
... print i,
...
1 2 3 4 5 6 7 8 9 10
>>>
#Interactive mode programming - #3
>>> name = raw_input("Enter a squence of numbers 1 by 1 in a single line : ")
Enter a squence of numbers 1 by 1 in a single line : 1 2 3 4 5 6 7 8 9 10
>>>
>>> print name #Printing string
1 2 3 4 5 6 7 8 9 10
>>>
>>> print type(name) #Printing type of variable named name
>>>
>>> l = [int(s) for s in name.split()] #Creating a list of numbers after extracting numbers from input string
>>> print l
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>