In [ ]:
# tuples
# tuples are readonly lists.
# tuple -> string + list

In [5]:
shirt_size = ('S','M','L','XL','XXL')
empty_tuple = tuple()
empty_tuple1 = ()

In [6]:
print shirt_size,type(shirt_size)
print empty_tuple,type(empty_tuple)
print empty_tuple1,type(empty_tuple1)


('S', 'M', 'L', 'XL', 'XXL') <type 'tuple'>
() <type 'tuple'>
() <type 'tuple'>

In [12]:
my_name = "python"
print my_name,type(my_name)
my_name = ("python")
print my_name,type(my_name)
my_name = ("python",)
print my_name,type(my_name)
my_name = "python","ruby","django","rails"
print my_name,type(my_name)


python <type 'str'>
python <type 'str'>
('python',) <type 'tuple'>
('python', 'ruby', 'django', 'rails') <type 'tuple'>

In [7]:
# indexing,slicing
print shirt_size[2]


L

In [8]:
# Tuple is immutable
shirt_size[1] = "m"


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-13b2d5d402c1> in <module>()
      1 # Tuple is immutable
----> 2 shirt_size[1] = "m"

TypeError: 'tuple' object does not support item assignment

In [16]:
# packing and unpacking
my_fruits = ("apple","banana","cherry","dates") # packing
a,b,c,d = my_fruits  # unpacking
print a
print b
print c
print d


apple
banana
cherry
dates

In [17]:
# RHS = LHS
e,f,g = my_fruits


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-1cd3cc195bff> in <module>()
----> 1 e,f,g = my_fruits

ValueError: too many values to unpack

In [18]:
e,f,g,h,i = my_fruits


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-91469893d8a2> in <module>()
----> 1 e,f,g,h,i = my_fruits

ValueError: need more than 4 values to unpack

In [19]:
# tuples vs lists
my_students = ['bhavani','laxmi','ram','raju','shyam','arbaz','sumant','anil']
my_exams    = ['python','django','puppet','chef','ansibel','openstack','flask','bottle']

In [23]:
name='bhavani'
print name
print name in my_students
print my_students.index(name)
print my_exams[ my_students.index(name)]


bhavani
True
0
python

In [25]:
name="bhavani"
if name in my_students:
    print "{} is going to give exam {}".format(name,my_exams[my_students.index(name)])


bhavani is going to give exam python

In [26]:
# principle
my_students.sort()
print my_students


['anil', 'arbaz', 'bhavani', 'laxmi', 'raju', 'ram', 'shyam', 'sumant']

In [28]:
name="bhavani"
if name in my_students:
    print "{} is going to give exam {}".format(name,my_exams[my_students.index(name)])


bhavani is going to give exam puppet

In [29]:
exam = [('bhavani','python'),('laxmi','django'),('ram','puppet'),('raju','chef'),('shyam','ansibel')]

In [32]:
exam[0] = ('bhavani','ruby')

In [33]:
print exam


[('bhavani', 'ruby'), ('laxmi', 'django'), ('ram', 'puppet'), ('raju', 'chef'), ('shyam', 'ansibel')]

In [34]:
exam[0][0]


Out[34]:
'bhavani'

In [35]:
exam[0][0] = 'Bhavani'


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-35-9867978cf761> in <module>()
----> 1 exam[0][0] = 'Bhavani'

TypeError: 'tuple' object does not support item assignment

In [38]:
print exam[3]
print exam[3][0],exam[3][1]
name,subject = exam[3]
print name,subject


('raju', 'chef')
raju chef
raju chef

In [41]:
name = 'raju'
for student,subject in exam:
    if name == student:
        print "{} is going to give exam {}".format(student,subject)


raju is going to give exam chef

In [42]:
print exam


[('bhavani', 'ruby'), ('laxmi', 'django'), ('ram', 'puppet'), ('raju', 'chef'), ('shyam', 'ansibel')]

In [43]:
# sort
exam.sort()

In [44]:
print exam


[('bhavani', 'ruby'), ('laxmi', 'django'), ('raju', 'chef'), ('ram', 'puppet'), ('shyam', 'ansibel')]

In [45]:
name = 'ram'
for student,subject in exam:
    if name == student:
        print "{} is going to give exam {}".format(student,subject)


ram is going to give exam puppet

In [47]:
# 
new = (['a','b'],['c','d'])
print new[0]
new[0] = [1,2]


['a', 'b']
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-47-f709ff016c53> in <module>()
      2 new = (['a','b'],['c','d'])
      3 print new[0]
----> 4 new[0] = [1,2]
      5 new[0][0]="one"

TypeError: 'tuple' object does not support item assignment

In [48]:
new[0][0]="one"
print new


(['one', 'b'], ['c', 'd'])

In [49]:
# function
print dir(shirt_size)


['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

In [51]:
print shirt_size.count('M')
print shirt_size.index('S')


1
0

In [ ]: