In [ ]:
# tuples
# read only lists
# ex: days of week,flipkart,jabong
In [1]:
# defination
weeks = ('sun','mon','tue','wed','thu','fri','sat')
In [2]:
print type(weeks),weeks
In [4]:
# use case 2
empty_tuple = tuple()
print empty_tuple,type(empty_tuple)
In [6]:
# use case 3
empty_tuple = ()
print empty_tuple,type(empty_tuple)
In [8]:
my_string = "python"
print my_string,type(my_string)
In [10]:
my_string = ("python")
print my_string,type(my_string)
In [11]:
my_string = ("python",)
print my_string,type(my_string)
In [12]:
my_string = "python","linux","java","ruby"
print my_string,type(my_string)
In [14]:
my_string = "python,linux,java,ruby"
print my_string,type(my_string)
In [15]:
# task
my_fruits = ['apple','banana','cherry','dates']
# my_fruits = ('apple','banana','cherry','dates')
In [16]:
print tuple(my_fruits)
In [17]:
# tuples - indexing and slicing.
In [ ]:
# tuples are immutable
In [18]:
print weeks
In [19]:
weeks[0]="sunday"
In [20]:
# packing and unpacking
# lists or tuples - packing.
In [21]:
my_weeks = ('sun', 'mon', 'tue', 'wed')
# a=sun,b=mon,c=tue,d=wed
In [22]:
a = my_weeks[0]
print a,type(a)
In [23]:
# unpacking
a,b,c,d = my_weeks
print a
print b
print c
print d
In [24]:
a,b,c,d,e = my_weeks
In [25]:
a,b,c = my_weeks
In [26]:
# lists and tuples - complex datastructures
In [27]:
my_student = ['viraja','naimisha','joel','deepti','madhav','hanu','rohit']
my_subjects = ['python','django','puppet','chef','dockers','kubernates','ansibel']
In [30]:
name='viraja'
print name in my_student
print my_student.index(name)
print my_subjects[my_student.index(name)]
In [33]:
#
name = raw_input("please enter your name:")
if name in my_student:
print "{} is going to give {}".format(name,my_subjects[my_student.index(name)])
In [34]:
# anti-climax
# principle
# junior
my_student.sort()
In [35]:
#my_student = ['viraja','naimisha','joel','deepti','madhav','hanu','rohit']
#my_subjects = ['python','django','puppet','chef','dockers','kubernates','ansibel']
print my_student
print my_subjects
In [36]:
name = raw_input("please enter your name:")
if name in my_student:
print "{} is going to give {}".format(name,my_subjects[my_student.index(name)])
In [39]:
# list of tuples
my_exams = [('viraja','python'),('naimisha','django'),('joel','puppet'),('deepti','chef'),('madhav','dockers')]
In [40]:
print my_exams
In [43]:
name = raw_input("please enter your name:")
for student,subject in my_exams:
if name == student:
print "{} is going to give exam {}".format(student,subject)
In [50]:
# anticlimax
# principle
my_exams.sort()
print my_exams
In [45]:
name = raw_input("please enter your name:")
for student,subject in my_exams:
if name == student:
print "{} is going to give exam {}".format(student,subject)
In [49]:
#
print my_exams[0][1]
my_exams[0][1]="aws"
In [51]:
my_exams[0] = ("deepthi","aws")
print my_exams
In [52]:
print weeks
In [53]:
print dir(weeks)
In [ ]: