In [ ]:
# tuples
# tuples are readonly representation of a list.

In [16]:
weeks = ('sun','mon','tue','wed','thu','fri','sat')
print weeks,type(weeks)


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

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


() <type 'tuple'>

In [3]:
empty_tuple = ()
print empty_tuple,type(empty_tuple)


() <type 'tuple'>

In [4]:
# support iteration
# indexing,slicing

In [ ]:
# packing and unpacking
# any list or tuple with values inside it is called a package.
#

In [5]:
# p = weeks[0]
p,q,r,s,t,u,v = weeks
print p,q,r,s,t,u,v


sun mon tue wed thu fri sat

In [6]:
# LHS != RHS
p,q,r,s,t,u = weeks


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-53aa8c17f2b9> in <module>()
      1 # RHS != LHS
----> 2 p,q,r,s,t,u = weeks

ValueError: too many values to unpack

In [7]:
p,q,r,s,t,u,v,w = weeks


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-17ad5318d731> in <module>()
----> 1 p,q,r,s,t,u,v,w = weeks

ValueError: need more than 7 values to unpack

In [8]:
# list and tuples
students = ['khiri','ajay','ajas','arun','venkat','rahul']
subject = ['python','django','puppet','chef','ajax','flask']

In [9]:
name='venkat'
print name in students


True

In [11]:
# rollno/index of venkat
print students.index(name)
print subject[students.index(name)]


4
ajax

In [15]:
# program
name = raw_input("please enter the name of the student:")
if name in students:
    print "{} is going to give the exam {}".format(name,subject[students.index(name)])
else:
    print "{} is not in the list".format(name)


please enter the name of the student:venkat
venkat is going to give the exam ajax

In [16]:
# sort
students.sort()
print students


['ajas', 'ajay', 'arun', 'khiri', 'rahul', 'venkat']

In [18]:
print subject


['python', 'django', 'puppet', 'chef', 'ajax', 'flask']

In [19]:
# program
name = raw_input("please enter the name of the student:")
if name in students:
    print "{} is going to give the exam {}".format(name,subject[students.index(name)])
else:
    print "{} is not in the list".format(name)


please enter the name of the student:venkat
venkat is going to give the exam flask

In [ ]:
#students = ['khiri','ajay','ajas','arun','venkat','rahul']
#subject = ['python','django','puppet','chef','ajax','flask']

In [1]:
exams = [('khiri','python'),('ajay','django'),('ajas','puppet'),('arun','chef')]

In [2]:
print exams


[('khiri', 'python'), ('ajay', 'django'), ('ajas', 'puppet'), ('arun', 'chef')]

In [15]:
# task
tasks = [('khiri', 'python'), ('ajay', 'django'), ('ajas', 'puppet'), ('arun', 'chef')]
print tasks[0][1]
#tasks[0][1]='Jython'
# you get the below error as you cannot modify the elements inside a tuple.
'''
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-20ae4ae5bf82> in <module>()
      2 tasks = [('khiri', 'python'), ('ajay', 'django'), ('ajas', 'puppet'), ('arun', 'chef')]
      3 print tasks[0][1]
----> 4 tasks[0][1]='Jython'

TypeError: 'tuple' object does not support item assignment


'''
# I was able to modify as tuple is an element of a list.

print tasks[0],type(tasks[0])
tasks[0]=('Khiri','Python')
print tasks


python
('khiri', 'python') <type 'tuple'>
[('Khiri', 'Python'), ('ajay', 'django'), ('ajas', 'puppet'), ('arun', 'chef')]

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


please enter the student name:ajas
ajas is going to give exam puppet

In [8]:
# sort on top of exams
exams.sort()
print exams


[('ajas', 'puppet'), ('ajay', 'django'), ('arun', 'chef'), ('khiri', 'python')]

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


please enter the student name:ajas
ajas is going to give exam puppet

In [19]:
# functions
print weeks
print weeks.count('sun')
print weeks.index('sun')


('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')
1
0

In [ ]: