In [ ]:
# tuples - readnonly representation of lists.
# gender -> (m,f)
# days of week - friday,monday

In [4]:
# create a tuple
my_gender = ('f','m')
print my_gender,type(my_gender)
empty_tuple = ()
print empty_tuple,type(empty_tuple)
empty_tuple = tuple()
print empty_tuple,type(empty_tuple)


('f', 'm') <type 'tuple'>
() <type 'tuple'>
() <type 'tuple'>

In [9]:
# 
my_string = "python"
print type(my_string)
my_string = ("python")
print type(my_string)
my_string = "python","linux","django","redhat"
print type(my_string)
my_string="python,linux,django,redhat"
print type(my_string)
my_string=("python",)
print type(my_string)


<type 'str'>
<type 'str'>
<type 'tuple'>
<type 'str'>
<type 'tuple'>

In [10]:
# tuple
# indexing,slicing

In [11]:
# packing and unpacking
# tuples and lists are packing.
my_week=("sun","mon","tue","wed","thu","fri","sat")
# s = my_week[0]
s,m,t,w,tu,f,sa = my_week
print s,m,t,w,tu,f,sa


sun mon tue wed thu fri sat

In [12]:
s,m,t,w,tu,f = my_week


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-8ce1b7e50828> in <module>()
----> 1 s,m,t,w,tu,f = my_week

ValueError: too many values to unpack

In [13]:
s,m,t,w,tu,f,sa,sw = my_week


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-ece0200f866c> in <module>()
----> 1 s,m,t,w,tu,f,sa,sw = my_week

ValueError: need more than 7 values to unpack

In [ ]:
# lists and tuple

In [14]:
students = ['pradeep','ravi','rahul','sudha','keerthan','akshit']
subjects = ['python','django','puppet','chef','ansibel','vagrant']

In [15]:
# how to verify is student is ther in list or not.
name = 'pradeep'
print name in students


True

In [17]:
# index for pradeep
print students.index('pradeep')


0

In [18]:
# subject pradeep going to write.
print subjects[students.index('pradeep')]


python

In [21]:
name = raw_input("please enter  your name:")
if name in students:
    print "{} is going to give the exam {}".format(name,subjects[students.index(name)])


please enter  your name:keerthan
keerthan is going to give the exam ansibel

In [ ]:
#students = ['pradeep','ravi','rahul','sudha','keerthan','akshit']
#subjects = ['python','django','puppet','chef','ansibel','vagrant]

In [22]:
subjects.sort()
print students
print subjects


['pradeep', 'ravi', 'rahul', 'sudha', 'keerthan', 'akshit']
['ansibel', 'chef', 'django', 'puppet', 'python', 'vagrant']

In [23]:
name = raw_input("please enter  your name:")
if name in students:
    print "{} is going to give the exam {}".format(name,subjects[students.index(name)])


please enter  your name:keerthan
keerthan is going to give the exam python

In [24]:
# list of tuples.
exams = [('pradeep','python'),('ravi','django'),('rahul','puppet'),('keerthan','ansible'),('akshit','vagrant')]

In [25]:
for value in exams:
    print value


('pradeep', 'python')
('ravi', 'django')
('rahul', 'puppet')
('keerthan', 'ansible')
('akshit', 'vagrant')

In [26]:
name = raw_input("please enter your name:")
for student,subject in exams:
    if name in student:
        print "{} is going to give exam {}".format(student,subject)


please enter your name:ravi
ravi is going to give exam django

In [27]:
exams.sort()

In [28]:
print exams # groupby function in databases.


[('akshit', 'vagrant'), ('keerthan', 'ansible'), ('pradeep', 'python'), ('rahul', 'puppet'), ('ravi', 'django')]

In [ ]:
# list of tuple
# tuples of list.

In [1]:
names = [('akshit', 'vagrant'), ('keerthan', 'ansible'), ('pradeep', 'python')]

In [2]:
print names[0][0]


akshit

In [3]:
names[0][0] = 'Akshit'


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-9a57173c2c41> in <module>()
----> 1 names[0][0] = 'Akshit'

TypeError: 'tuple' object does not support item assignment

In [4]:
names[0] = ('Akshit','vagrant')

In [5]:
print names


[('Akshit', 'vagrant'), ('keerthan', 'ansible'), ('pradeep', 'python')]

In [8]:
names = (['akshit', 'vagrant'], ['keerthan', 'ansible'], ['pradeep', 'python'])

In [9]:
names[0][0] = "Akshit"

In [10]:
print names


(['Akshit', 'vagrant'], ['keerthan', 'ansible'], ['pradeep', 'python'])

In [11]:
names[0] = ['kumar','ansibel']
print names


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-d5fc888a04d9> in <module>()
----> 1 names[0] = ['kumar','ansibel']
      2 print names

TypeError: 'tuple' object does not support item assignment

In [13]:
#task
my_list = [1,2,3,4,5]
# output (1,2,3,4,5)
my_tuple = tuple(my_list)
print my_tuple
print my_list


(1, 2, 3, 4, 5)
[1, 2, 3, 4, 5]

In [ ]: