In [ ]:
# tuples
# tuples are readonly lists.
In [1]:
# creation of tuples
weeks = ('sun','mon','tue','wed','thu','sat')
print weeks,type(weeks)
In [2]:
my_empty = ()
print my_empty,type(my_empty)
my_empty = tuple()
print my_empty,type(my_empty)
In [3]:
#
my_string = "python"
print my_string,type(my_string)
In [4]:
my_string = ("python")
print my_string,type(my_string)
In [5]:
my_string = ("python",)
print my_string,type(my_string)
In [6]:
my_string = "linux","sol","aix","hpux"
print my_string,type(my_string)
In [ ]:
# tuples - indexing,slicing
In [8]:
# tuples are immutable. you cannot modify
print weeks[0]
weeks[0]="Sun"
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
In [11]:
s,m,t,w,th,fr,sa=weeks
In [12]:
s,m,t,w,th=weeks
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)]
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)])
In [19]:
# anti-climax
# principle
# junior
print students.sort()
print students
print exams
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)])
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])
In [9]:
# change to ('Sushma','Python')
print Dexams[0]
Dexams[0]=('Sushma','Python')
print Dexams[0]
Dexams[0][1]="ruby"
In [11]:
for value in Dexams:
print value
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)
In [14]:
# anti-climax
# principle
Dexams.sort()
In [15]:
# groupby in our database.
print Dexams
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)
In [ ]: