In [ ]:
# tuples
# tuples are readonly represenation of lists.
# days of week,number of months,gender

In [3]:
my_week = ('sun','mon','tue','wed','thu','fri','sat')
print my_week,type(my_week)

my_empty = ()
print my_empty,type(my_empty)

my_empty = tuple()
print my_empty,type(my_empty)


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

In [7]:
# indendation,slicing,indented data structures.

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 = "linux","sol","aix","hpux"
print my_string,type(my_string)


python <type 'str'>
python <type 'str'>
('python',) <type 'tuple'>
('linux', 'sol', 'aix', 'hpux') <type 'tuple'>

In [8]:
# packing and unpacking
# list and tuples

my_week = ('sun','mon','tue','wed') # packing
sunday = my_week[0]
print sunday


sun

In [9]:
# unpacking
s,m,t,w = my_week
print s,m,t,w


sun mon tue wed

In [10]:
s,m,t = my_week


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-ead744de2409> in <module>()
----> 1 s,m,t = my_week

ValueError: too many values to unpack

In [11]:
s,m,t,w,th = my_week


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-76a0e1b6fea7> in <module>()
----> 1 s,m,t,w,th = my_week

ValueError: need more than 4 values to unpack

In [12]:
# list and tuples

my_students = ['ravali','hemant','abhi','priya','sujani','pridhvi']
my_exams =    ['python','linux','django','puppet','chef','ansibel']

In [14]:
name='ravali'
print my_students.index(name) #0
print my_exams[my_students.index(name)]


0
python

In [16]:
name = raw_input("please enter your name:")
if name in my_students:
    print "{} is going to give exam {}".format(name,my_exams[my_students.index(name)])


please enter your name:priya
priya is going to give exam puppet

In [17]:
# junior
# anti-climax - principle
my_students.sort()
print my_students
print my_exams


['abhi', 'hemant', 'pridhvi', 'priya', 'ravali', 'sujani']
['python', 'linux', 'django', 'puppet', 'chef', 'ansibel']

In [18]:
name = raw_input("please enter your name:")
if name in my_students:
    print "{} is going to give exam {}".format(name,my_exams[my_students.index(name)])


please enter your name:ravali
ravali is going to give exam chef

In [21]:
# list of tuples
my_exams = [('ravali','python'),('hemant','linux'),('abhi','django'),('priya','puppet'),('sujani','chef'),('pridhvi','ansibel')]

In [22]:
print my_exams


[('ravali', 'python'), ('hemant', 'linux'), ('abhi', 'django'), ('priya', 'puppet'), ('sujani', 'chef'), ('pridhvi', 'ansibel')]

In [28]:
print type(my_exams)
print type(my_exams[0])
my_exams[0]=('Ravali','python')
print my_exams
print my_exams[0][1]
my_exams[0][1]="flask"


<type 'list'>
<type 'tuple'>
[('Ravali', 'python'), ('hemant', 'linux'), ('abhi', 'django'), ('priya', 'puppet'), ('sujani', 'chef'), ('pridhvi', 'ansibel')]
python
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-28-3d22d2654646> in <module>()
      4 print my_exams
      5 print my_exams[0][1]
----> 6 my_exams[0][1]="flask"

TypeError: 'tuple' object does not support item assignment

In [29]:
my_exams.sort()
print my_exams


[('Ravali', 'python'), ('abhi', 'django'), ('hemant', 'linux'), ('pridhvi', 'ansibel'), ('priya', 'puppet'), ('sujani', 'chef')]

In [31]:
name="abhi"
for stu,sub in my_exams:
    if stu == name:
        print "{} is giving exam {}".format(stu,sub)


abhi is giving exam django

In [33]:
# exercise
fruits = (['a','apple'],['b','banana'],['c','cherry'])

In [36]:
fruits[0][1]='apricot'
print fruits
fruits[1] = ['b','blue berry']


(['a', 'apricot'], ['b', 'banana'], ['c', 'cherry'])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-36-cb326825568d> in <module>()
      1 fruits[0][1]='apricot'
      2 print fruits
----> 3 fruits[1] = ['b','blue berry']

TypeError: 'tuple' object does not support item assignment

In [ ]:
# Python challenges
# http://www.pythonchallenge.com/pc/def/map.html
# https://projecteuler.net/archives