In [ ]:
# tuples 
# tuples are readonly lists.

In [1]:
# creation of tuples
weeks = ('sun','mon','tue','wed','thu','sat')
print weeks,type(weeks)


('sun', 'mon', 'tue', 'wed', 'thu', 'sat') <type 'tuple'>

In [2]:
my_empty = ()
print my_empty,type(my_empty)
my_empty = tuple()
print my_empty,type(my_empty)


() <type 'tuple'>
() <type 'tuple'>

In [3]:
#
my_string = "python"
print my_string,type(my_string)


python <type 'str'>

In [4]:
my_string = ("python")
print my_string,type(my_string)


python <type 'str'>

In [5]:
my_string = ("python",)
print my_string,type(my_string)


('python',) <type 'tuple'>

In [6]:
my_string = "linux","sol","aix","hpux"
print my_string,type(my_string)


('linux', 'sol', 'aix', 'hpux') <type 'tuple'>

In [ ]:
# tuples - indexing,slicing

In [8]:
# tuples are immutable. you cannot modify
print weeks[0]
weeks[0]="Sun"


sun
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-0cc9f8761ee9> in <module>()
      1 # tuples are immutable. you cannot modify
      2 print weeks[0]
----> 3 weeks[0]="Sun"

TypeError: 'tuple' object does not support item assignment

In [10]:
# packing and unpacking
# list and tuple are both packing
weeks = ('sun','mon','tue','wed','thu','sat')
s,m,t,w,th,sa=weeks
print s,m,t,w,th,sa


sun mon tue wed thu sat

In [11]:
s,m,t,w,th,fr,sa=weeks


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-c11572eafab0> in <module>()
----> 1 s,m,t,w,th,fr,sa=weeks

ValueError: need more than 6 values to unpack

In [12]:
s,m,t,w,th=weeks


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-740b6de4fe1e> in <module>()
----> 1 s,m,t,w,th=weeks

ValueError: too many values to unpack

In [13]:
# tuples and lists

students = ['sushma','shalini','mahi','naren','sam']
exams   =  ['python','django','ruby','puppet','chef']

In [16]:
name='sushma'
print name in students
print students.index(name)
print exams[students.index(name)]


True
0
python

In [18]:
name = raw_input("please enter the name of student:")
if name in students:
    print "{} is going to give exam {}".format(name,exams[students.index(name)])


please enter the name of student:sam
sam is going to give exam chef

In [19]:
# anti-climax
# principle
# junior
print students.sort()
print students
print exams


None
['mahi', 'naren', 'sam', 'shalini', 'sushma']
['python', 'django', 'ruby', 'puppet', 'chef']

In [21]:
name = raw_input("please enter the name of student:")
if name in students:
    print "{} is going to give exam {}".format(name,exams[students.index(name)])


please enter the name of student:sam
sam is going to give exam ruby

In [4]:
# list of tuples
students = ['sushma','shalini','mahi','naren','sam']
exams   =  ['python','django','ruby','puppet','chef']

Dexams = [('sushma','python'),('shalini','django'),('mahi','ruby'),('naren','puppet'),('sam','chef')]

In [5]:
print type(Dexams)
print type(Dexams[0])
print type(Dexams[0][0])


<type 'list'>
<type 'tuple'>
<type 'str'>

In [9]:
# change to ('Sushma','Python')
print Dexams[0]
Dexams[0]=('Sushma','Python')
print Dexams[0]
Dexams[0][1]="ruby"


('Sushma', 'Python')
('Sushma', 'Python')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-33854b6567a3> in <module>()
      3 Dexams[0]=('Sushma','Python')
      4 print Dexams[0]
----> 5 Dexams[0][1]="ruby"

TypeError: 'tuple' object does not support item assignment

In [11]:
for value in Dexams:
     print value


('Sushma', 'Python')
('shalini', 'django')
('mahi', 'ruby')
('naren', 'puppet')
('sam', 'chef')

In [13]:
name = raw_input("please give the name of the student:")
for stu,sub in Dexams:
    if name == stu:
        print "{} is going to give {}".format(stu,sub)


please give the name of the student:mahi
mahi is going to give ruby

In [14]:
# anti-climax
# principle
Dexams.sort()

In [15]:
# groupby in our database.
print Dexams


[('Sushma', 'Python'), ('mahi', 'ruby'), ('naren', 'puppet'), ('sam', 'chef'), ('shalini', 'django')]

In [16]:
name = raw_input("please give the name of the student:")
for stu,sub in Dexams:
    if name == stu:
        print "{} is going to give {}".format(stu,sub)


please give the name of the student:naren
naren is going to give puppet

In [ ]: