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)
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)
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
In [12]:
s,m,t,w,tu,f = my_week
In [13]:
s,m,t,w,tu,f,sa,sw = my_week
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
In [17]:
# index for pradeep
print students.index('pradeep')
In [18]:
# subject pradeep going to write.
print subjects[students.index('pradeep')]
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)])
In [ ]:
#students = ['pradeep','ravi','rahul','sudha','keerthan','akshit']
#subjects = ['python','django','puppet','chef','ansibel','vagrant]
In [22]:
subjects.sort()
print students
print subjects
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)])
In [24]:
# list of tuples.
exams = [('pradeep','python'),('ravi','django'),('rahul','puppet'),('keerthan','ansible'),('akshit','vagrant')]
In [25]:
for value in exams:
print value
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)
In [27]:
exams.sort()
In [28]:
print exams # groupby function in databases.
In [ ]:
# list of tuple
# tuples of list.
In [1]:
names = [('akshit', 'vagrant'), ('keerthan', 'ansible'), ('pradeep', 'python')]
In [2]:
print names[0][0]
In [3]:
names[0][0] = 'Akshit'
In [4]:
names[0] = ('Akshit','vagrant')
In [5]:
print names
In [8]:
names = (['akshit', 'vagrant'], ['keerthan', 'ansible'], ['pradeep', 'python'])
In [9]:
names[0][0] = "Akshit"
In [10]:
print names
In [11]:
names[0] = ['kumar','ansibel']
print names
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
In [ ]: