In [ ]:
# tuples
# tuples are readonly representation of a list.
In [16]:
weeks = ('sun','mon','tue','wed','thu','fri','sat')
print weeks,type(weeks)
In [2]:
empty_tuple = tuple()
print empty_tuple,type(empty_tuple)
In [3]:
empty_tuple = ()
print empty_tuple,type(empty_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
In [6]:
# LHS != RHS
p,q,r,s,t,u = weeks
In [7]:
p,q,r,s,t,u,v,w = weeks
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
In [11]:
# rollno/index of venkat
print students.index(name)
print subject[students.index(name)]
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)
In [16]:
# sort
students.sort()
print students
In [18]:
print subject
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)
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
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
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)
In [8]:
# sort on top of exams
exams.sort()
print exams
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)
In [19]:
# functions
print weeks
print weeks.count('sun')
print weeks.index('sun')
In [ ]: