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


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

In [4]:
# use case 2
empty_tuple = tuple()
print empty_tuple,type(empty_tuple)


() <type 'tuple'>

In [6]:
# use case 3
empty_tuple = ()
print empty_tuple,type(empty_tuple)


() <type 'tuple'>

In [8]:
my_string = "python"
print my_string,type(my_string)


python <type 'str'>

In [10]:
my_string = ("python")
print my_string,type(my_string)


python <type 'str'>

In [11]:
my_string = ("python",)
print my_string,type(my_string)


('python',) <type 'tuple'>

In [12]:
my_string = "python","linux","java","ruby"
print my_string,type(my_string)


('python', 'linux', 'java', 'ruby') <type 'tuple'>

In [14]:
my_string = "python,linux,java,ruby"
print my_string,type(my_string)


python,linux,java,ruby <type 'str'>

In [15]:
# task
my_fruits = ['apple','banana','cherry','dates']
# my_fruits = ('apple','banana','cherry','dates')

In [16]:
print tuple(my_fruits)


('apple', 'banana', 'cherry', 'dates')

In [17]:
# tuples - indexing and slicing.

In [ ]:
# tuples are immutable

In [18]:
print weeks


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

In [19]:
weeks[0]="sunday"


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-cf642cc5226b> in <module>()
----> 1 weeks[0]="sunday"

TypeError: 'tuple' object does not support item assignment

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)


sun <type 'str'>

In [23]:
# unpacking
a,b,c,d = my_weeks
print a
print b
print c
print d


sun
mon
tue
wed

In [24]:
a,b,c,d,e = my_weeks


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-580ba9e35292> in <module>()
----> 1 a,b,c,d,e = my_weeks

ValueError: need more than 4 values to unpack

In [25]:
a,b,c = my_weeks


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-beb7bfd6c989> in <module>()
----> 1 a,b,c = my_weeks

ValueError: too many values to unpack

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)]


True
0
python

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)])


please enter your name:rohit
rohit is going to give ansibel

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


['deepti', 'hanu', 'joel', 'madhav', 'naimisha', 'rohit', 'viraja']
['python', 'django', 'puppet', 'chef', 'dockers', 'kubernates', 'ansibel']

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)])


please enter your name:deepti
deepti is going to give python

In [39]:
# list of tuples
my_exams = [('viraja','python'),('naimisha','django'),('joel','puppet'),('deepti','chef'),('madhav','dockers')]

In [40]:
print my_exams


[('viraja', 'python'), ('naimisha', 'django'), ('joel', 'puppet'), ('deepti', 'chef'), ('madhav', 'dockers')]

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)


please enter your name:madhav
madhav is going to give exam dockers

In [50]:
# anticlimax
# principle
my_exams.sort()
print my_exams


[('deepti', 'chef'), ('joel', 'puppet'), ('madhav', 'dockers'), ('naimisha', 'django'), ('viraja', 'python')]

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)


please enter your name:madhav
madhav is going to give exam dockers

In [49]:
#
print my_exams[0][1]
my_exams[0][1]="aws"


chef
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-49-ab9185871157> in <module>()
      1 #
      2 print my_exams[0][1]
----> 3 my_exams[0][1]="aws"

TypeError: 'tuple' object does not support item assignment

In [51]:
my_exams[0] = ("deepthi","aws")
print my_exams


[('deepthi', 'aws'), ('joel', 'puppet'), ('madhav', 'dockers'), ('naimisha', 'django'), ('viraja', 'python')]

In [52]:
print weeks


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

In [53]:
print dir(weeks)


['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

In [ ]: