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)


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

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)


python <type 'str'>
python <type 'str'>
('python',) <type 'tuple'>
('python', 'perl', 'shell', 'linux') <type 'tuple'>

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'


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-32430e616bae> in <module>()
      1 my_string ="python"
----> 2 my_string[0] = 'T'

TypeError: 'str' object does not support item assignment

In [12]:
days_of_week = ['sun','mon','tue','wed','thu','fri','sat']
days_of_week[0] = "SUN"
print days_of_week


['SUN', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']

In [13]:
days_of_week = ('sun','mon','tue','wed','thu','fri','sat')
days_of_week[0] = "SUN"


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-a86265b9f377> in <module>()
      1 days_of_week = ('sun','mon','tue','wed','thu','fri','sat')
----> 2 days_of_week[0] = "SUN"

TypeError: 'tuple' object does not support item assignment

In [16]:
# string - sequence of characters
my_string = "python,perl,shell,linux"
print my_string,type(my_string)
print my_string[0]


python,perl,shell,linux <type 'str'>
p

In [17]:
# tuple - sequence of values
my_string = ("python","perl","shell","linux")
print my_string,type(my_string)
print my_string[0]


('python', 'perl', 'shell', 'linux') <type 'tuple'>
python

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)


python <type 'str'>
perl <type 'str'>
shell <type 'str'>
linux <type 'str'>

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


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-57001ff85a95> in <module>()
      1 # while unpacking .. elements on left hand side should be equal to elements on right hand side.
      2 # LHS = RHS
----> 3 a,b,c = ("python","perl","shell","linux")

ValueError: too many values to unpack

In [20]:
a,b,c,d,e = ("python","perl","shell","linux")


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-182658a2351c> in <module>()
----> 1 a,b,c,d,e = ("python","perl","shell","linux")

ValueError: need more than 4 values to unpack

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


True
False
0
python

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


kumar is going to give the exam ansibel

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


['avinash', 'azhar', 'kumar', 'vipin']
['python', 'django', 'ruby', 'ansibel']

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


kumar is going to give the exam ruby

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)


azhar is going to give exam ruby

In [34]:
name = 'avinash'

for student,subject in new_exams:
    if student == name:
        print "{} is going to give exam {}".format(student,subject)


avinash is going to give exam django

In [35]:
# anti-climax - sorting
new_exams.sort()
print new_exams


[('avinash', 'django'), ('azhar', 'ruby'), ('kumar', 'ansibel'), ('vipin', 'python')]

In [36]:
name = 'vipin'

for student,subject in new_exams:
    if student == name:
        print "{} is going to give exam {}".format(student,subject)


vipin is going to give exam python

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)


('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')
['__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 [39]:
print days_of_week.count('sun') # 1
print days_of_week.index('wed') # 3


1
3

In [ ]: