In [ ]:
# Tuples
# Tuples are readonly representation of list like data.
# days of weeks - 7 days - sun,mon,tue,wed,thu,fri,sat
In [5]:
# how to create a tuple.
days_of_week = ('sun','mon','tue','wed','thu','fri','sat')
print days_of_week,type(days_of_week)
empty_tuple = tuple()
print empty_tuple,type(empty_tuple)
empty_tuple1 = ()
print empty_tuple1,type(empty_tuple1)
In [9]:
# what is tuple?
my_string = "python"
print my_string,type(my_string)
my_string = ("python")
print my_string,type(my_string)
my_string = ("python",)
print my_string,type(my_string)
my_string = "python","perl","shell","linux"
print my_string,type(my_string)
In [10]:
# lists vs tuples vs string
# strings are sequence of characters. you cannot index a comma seperated strings.
# my_string = "python" # true
# my_string = "python,perl,shell,linux" # true
# my_string = "python","perl","shell","linux" # False
# you cannot modify the contents of a string.
# List is a sequence of values , which are indexed and are read-write.
# days_of_week = ['sun','mon','tue','wed','thu','fri','sat']
# you can modify the elements of a list.
# Tuple is a sequence of values , which are indexed but are readonly.
# days_of_week = ('sun','mon','tue','wed','thu','fri','sat')
# you cannot modify the elements of a tuple
In [11]:
my_string ="python"
my_string[0] = 'T'
In [12]:
days_of_week = ['sun','mon','tue','wed','thu','fri','sat']
days_of_week[0] = "SUN"
print days_of_week
In [13]:
days_of_week = ('sun','mon','tue','wed','thu','fri','sat')
days_of_week[0] = "SUN"
In [16]:
# string - sequence of characters
my_string = "python,perl,shell,linux"
print my_string,type(my_string)
print my_string[0]
In [17]:
# tuple - sequence of values
my_string = ("python","perl","shell","linux")
print my_string,type(my_string)
print my_string[0]
In [26]:
# packing and unpacking
# lists and tuples are called packing. () or []
# ("python","perl","shell","linux") -> a packing
# unpacking
a,b,c,d = ("python","perl","shell","linux")
print a,type(a)
print b,type(b)
print c,type(c)
print d,type(d)
In [19]:
# while unpacking .. elements on left hand side should be equal to elements on right hand side.
# LHS = RHS
a,b,c = ("python","perl","shell","linux")
In [20]:
a,b,c,d,e = ("python","perl","shell","linux")
In [24]:
# lists and tuples
my_students = ['vipin','avinash','azhar','kumar']
my_exams = ['python','django','ruby','ansibel']
# logic
#1) if the student name is part of the my_students list.
name='vipin'
print 'vipin' in my_students
print 'rajni' in my_students
#2) corelate the student id to that of the my_exams
# getting the index of the student from my_students list
print my_students.index('vipin')
print my_exams[my_students.index('vipin')]
In [27]:
# name of the student.
name = 'kumar'
if name in my_students:
print "{} is going to give the exam {}".format(name,my_exams[my_students.index(name)])
In [28]:
# principle - anti-climax
my_students.sort()
print my_students
print my_exams
In [29]:
# name of the student.
name = 'kumar'
if name in my_students:
print "{} is going to give the exam {}".format(name,my_exams[my_students.index(name)])
In [33]:
#my_students = ['vipin','avinash','azhar','kumar']
#my_exams = ['python','django','ruby','ansibel']
# list of tuples
name = 'azhar'
new_exams = [('vipin','python'),('avinash','django'),('azhar','ruby'),('kumar','ansibel')]
for student,subject in new_exams:
if student == name:
print "{} is going to give exam {}".format(student,subject)
In [34]:
name = 'avinash'
for student,subject in new_exams:
if student == name:
print "{} is going to give exam {}".format(student,subject)
In [35]:
# anti-climax - sorting
new_exams.sort()
print new_exams
In [36]:
name = 'vipin'
for student,subject in new_exams:
if student == name:
print "{} is going to give exam {}".format(student,subject)
In [ ]:
# managing the data with right datastructures.
#
In [38]:
# tuples
days_of_week = ('sun','mon','tue','wed','thu','fri','sat')
print days_of_week
print dir(days_of_week)
In [39]:
print days_of_week.count('sun') # 1
print days_of_week.index('wed') # 3
In [ ]: