Objects of Python : Tuples




What are Tuples?
Just go through following operations.

>>> t1 = (1,'two',3) #declaring tuple
>>> t1
(1, 'two', 3)
>>> print t1 #does the same thing as above
(1, 'two', 3)
>>> type(t1) #lets check the type of tuple
<type 'tuple'>




How to declare a Singleton Tuple (Tuple with only one element) :

>>> t2 = (5) #trying to declare a singleton tuple
>>> t2
5
>>> type(t2) #Oops! We didn't expect it to be an int type
<type 'int'>
>>> t2 = (5,) #Note comma after 5
>>> t2
(5,)
>>> type(t2) #Now you're talking!!
<type 'tuple'>


We can even have a tuple inside a tuple (Nested Tuples).

>>> t3 = (t1,4,t2)
>>> t3
((1, 'two', 3), 4, (5,))


We can add(concatenate) two or more tuples

>>> t4 = (t1 + (4,) + t2)
>>> t4
(1, 'two', 3, 4, 5)


Indexing and Slicing are same as how we do with strings

>>> t4
(1, 'two', 3, 4, 5)
>>> t4[3] #Indexing
4
>>> t4[2:4] #Slicing
(3, 4)

Related Articles:

Operations on String: Concatenation, Indexing & Slicing
Python Objects: Dictionary (dict)
Python Objects: Lists, its use, Mutation of Lists

Note:
This is a part of what I learn in an online Open Course Ware offered by MIT on edX
Its for my personal reference & also for those who have missed the course.
You too can enroll yourself on edX (if they are still offering the course MITx 6.00x)